/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:
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 >>>