Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regular expression
#1
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.
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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.
Reply
#4
Yes, because ABC_ != SYS_

regex = r"\[(\w+)_(\d+)\] ([\w\s]+) \[END\]"
Now with 3 groups. Try it first on regex101.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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.
Reply
#6
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
Thank for help, I am still working on, i will keep posted. Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regular Expression stahorse 9 1,459 Jul-31-2023, 01:20 PM
Last Post: Will_Robertson
  regular expression pramod 1 1,521 Jul-10-2020, 06:38 AM
Last Post: karkas
  regular expression pramod 4 2,038 Jun-16-2020, 02:01 AM
Last Post: pramod
  regular expression pramod 2 1,600 May-05-2020, 02:36 AM
Last Post: pramod

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020