Python Forum
regX find XYZ when it occurs after ABC with stuff inbetween?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regX find XYZ when it occurs after ABC with stuff inbetween?
#2
/ABC[^XYZ]*(XYZ)/
I really like using regex pal to test these things out, while you play around with them to find something that works: http://www.regexpal.com/

Starting with ABC, any number of things that aren't XYZ, and ending with XYZ (there's probably a way to do it with forward-lookahead match groups, but my regex fu isn't that powerful).

Testing it out:
>>> import re
>>> tests = [
... 'lmnoqABCrstuvwXYZdefghijk)',
... 'missing_start_groupXYZ',
... 'missing_end_ABC_group',
... 'missing_both'
... ]
>>> for test in tests:
...   match = re.search("ABC[^XYZ]*(XYZ)", test, re.IGNORECASE)
...   print(match)
...   if match:
...     print(match.groups())
...
<_sre.SRE_Match object; span=(5, 17), match='ABCrstuvwXYZ'>
('XYZ',)
None
None
None
>>>
Reply


Messages In This Thread
RE: regX find XYZ when it occurs after ABC with stuff inbetween? - by nilamo - Aug-14-2017, 02:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Web Scraping Stuff Korgik 2 1,178 Dec-08-2022, 02:21 PM
Last Post: Korgik

Forum Jump:

User Panel Messages

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