HI i have question on regular expression
i am trying to match the text using re.search
example: original text = '[SYS_1] system application [END]'
Api text = '[ABCD_1] application [END]'
i use to match original like : found_text = re.search(REG_EXP, text)
REG_EXP = r"(\[\s*SYS_\d+\s\*].*\[\s*END\s*\])
How do we match using same or modify the regular expression for Api text.
Any one can help.
Try this one:
regex = r"\[SYS_(\d+)\] ([\w\s]+) \[END\]"
It has two groups. The first group is the number of sys_x
and the second group is the text.
It was work for first case: original text = '[SYS_1] system application [END]'
regex = r"\[SYS_(\d+)\] ([\w\s]+) \[END\]"
second case : It's matching both texts: original text as well Api text.
regex = r"([\w\s]+) \[END\]"
We need to get the second text only. how do we modify it to capture Api text only.
Yes, because ABC_ != SYS_
regex = r"\[(\w+)_(\d+)\] ([\w\s]+) \[END\]"
Now with 3 groups. Try it first on
regex101.
HI, Thanks for response and i appreciate for help. I am adding some more information for matching the patterns:
For example:
Text1 = [SYS_1] application [END]
Text2 = [Mobile_2] phone [END]
Text3 = [ABCD_1] text [END]
Text4 = [ABC_] some text [END]
Text5 = [ONE_N] some text [END]
.
.
TextN.......
For each Text needs to check with Regular Expression and match it
Here we need to match with Regular Expression to capture or extract the Text, looks like:
Now, write a one Regular expression for all Text:
Conditions : if SYS_1 and Mobile_2 match , then this are exclude.
if match Text3 or Text4 or Text5 these needs to capture or extract.
For me, it's effortless to create and test a new regex. If you don't do it by yourself, you learn nothing.
regex = r"\[(\w+)_([\w\d]?)\] ([\w\s]+) \[END\]"
The check if SYS_1 & Mobile_2 should be done with program logic (if-statements) and not via regex.
You can also reduce the groups if you change the parenthesis.
regex = r"\[([\w\d]+)\] ([\w\s\d]+) \[END\]"
With this regex you've two groups.
The rest is program logic.
Thank for help, I am still working on, i will keep posted. Thanks.