From 9a78c304c4f422f74af87c76c2cac680530ca75f Mon Sep 17 00:00:00 2001 From: Nayan <33187059+GShadow5@users.noreply.github.com> Date: Tue, 26 Aug 2025 12:35:38 -0400 Subject: [PATCH] add regex test program and another expression --- config.json.example | 7 ++++++- regextest.py | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 regextest.py diff --git a/config.json.example b/config.json.example index 3ea0243..2384259 100644 --- a/config.json.example +++ b/config.json.example @@ -7,7 +7,12 @@ { "type": "groups", "match": "\\[(.*?)\\].*INFO.*\\]: <(.*)> (.*)", - "capture": "**[{groups[0]}]**: {groups[2]}" + "capture": "**[{groups[1]}]**: {groups[2]}" + }, + { + "type": "groups", + "match": "\\[(.*?)\\].*WARN.*?\\] \\[(.*?)\\]: (.*)", + "capture": "**[{groups[1]}]**: {groups[2]}" } ] } diff --git a/regextest.py b/regextest.py new file mode 100644 index 0000000..9f3ab91 --- /dev/null +++ b/regextest.py @@ -0,0 +1,27 @@ +# Load data from testdata.txt +# Read each line and match it against the regex +# Print the match + +import re +import json + +REGEXS = [] +CAPTURES = [] + +with open('config.json.example', 'r') as f: + config = json.load(f) + for regex in config['config']['regex']: + REGEXS.append(regex['match']) + CAPTURES.append(regex['capture']) + +with open('testdata.txt', 'r') as f: + for line in f: + for i in range(len(REGEXS)): + REGEX = REGEXS[i] + CAPTURE = CAPTURES[i] + match = re.match(REGEX, line) + if match: + # print(match.group(1), match.group(2), match.group(3)) + groups = match.groups() + ouput = CAPTURE.format(groups=groups) + print(ouput) \ No newline at end of file