Below is my code:
import re
txt="""
Line 677: 05/01/2023 05:38:46 Windows OS backup of test226693 (test.env.dev.os.wn.sev.test226693.ep2) succeeded
Line 683: 05/01/2023 05:38:46 Windows OS backup of test226745 (test.env.dev.os.wn.sev.test226745.ep2) succeeded
Line 689: 05/01/2023 05:38:46 Windows OS backup of test226825 (test.env.dev.os.wn.sev.test226825.ep2) succeeded
Line 695: 05/01/2023 05:38:46 Windows OS backup of test226889 (test.env.dev.os.wn.sev.test226889.ep2) succeeded
Line 701: 05/01/2023 05:38:46 Windows OS backup of test227082 (test.env.dev.os.wn.sev.test227082.ep2) succeeded
Line 707: 05/01/2023 05:38:46 Windows OS backup of test227102 (test.env.dev.os.wn.sev.test227102.ep2) succeeded
Line 713: 05/01/2023 05:38:46 Windows OS backup of test227159 (test.env.dev.os.wn.sev.test227159.ep2) succeeded
Line 719: 05/01/2023 05:38:46 Windows OS backup of test227203 (test.env.dev.os.wn.sev.test227203.ep2) succeeded
"""
pattern=re.compile(r'of(\s\w+\s\(.*?\))')
res=pattern.findall(txt)
for i in res:
print(i)
Below is my output for the above code..
test226693 (test.env.dev.os.wn.sev.test226693.ep2)
test226745 (test.env.dev.os.wn.sev.test226745.ep2)
test226825 (test.env.dev.os.wn.sev.test226825.ep2)
test226889 (test.env.dev.os.wn.sev.test226889.ep2)
test227082 (test.env.dev.os.wn.sev.test227082.ep2)
test227102 (test.env.dev.os.wn.sev.test227102.ep2)
test227159 (test.env.dev.os.wn.sev.test227159.ep2)
test227203 (test.env.dev.os.wn.sev.test227203.ep2)
But would like to have a regular expression so that my output looks like below, basically to remove the brackets/parenthesis. Any help would be much appreciated.
test226693 test.env.dev.os.wn.sev.test226693.ep2
test226745 test.env.dev.os.wn.sev.test226745.ep2
test226825 test.env.dev.os.wn.sev.test226825.ep2
test226889 test.env.dev.os.wn.sev.test226889.ep2
test227082 test.env.dev.os.wn.sev.test227082.ep2
test227102 test.env.dev.os.wn.sev.test227102.ep2
test227159 test.env.dev.os.wn.sev.test227159.ep2
test227203 test.env.dev.os.wn.sev.test227203.ep2
The problem is your regex expression. You can check them for example using /regex101.com.
In your case you create on big group inside your match containing the
testXXXXX (test.env.dev.os.wn.sev.testXXXXXX.ep2)
.
What you want to do instead is creating a group for each substring you need. The regex would be something like of\s(\w+)\s\((.*?)\)
.
Now you just need to handle that res is a list of list, so join the lists and you get the output you want.
pattern=re.compile(r'of\s(\w+)\s\((.*?)\)')
res=pattern.findall(txt)
for i in res:
print(' '.join(i))
Note: posted at the same time as @simpleApp commented :)