Part 1
I haven’t found a solution yet; look at it a little more.
Answer to part 2
Here is a solution that works for regex engines that allow backlinks in lookbehinds like .NET and Matthew Barnett are a great regex module for Python.
In your example:
(?x) (?=(12345|abc))(?=(123|abcde))
Generally:
(?x) (?=(expr1))(?=(expr2))
I think this works ... but maybe there is a marginal case that I did not think about.
Here are some proven Python codes.
import regex pattern = r'''(?x) (?=(12345|abc))(?=(123|abcde)) # AND the expressions (?: # take the longest match \1(?<=\2.*) #abcde, \1 is abc | \2(?<=\1.*) #12345, \2 is 123 ) ''' myregex = regex.compile(pattern) print ("--- blah12345blahabcdeblah ---") for match in myregex.finditer("blah12345blahabcdeblah"): print("Overall match: ", match.group(0)) print ("--- blah123blahabcblah ---") for match in myregex.finditer("blah123blahabcblah"): print("Overall match: ", match.group(0)) print ("--- blah12345blahabcdeblah12345 ---") for match in myregex.finditer("blah12345blahabcdeblah12378"): print("Overall match: ", match.group(0))
source share