With the harder string Satoru added, this works:
>>> import re
>>> a = "one two three four five six one three four seven two"
>>> re.findall("one(?!.*two.*four).*four", a)
['one three four']
But - someday - you're really going to regret writing tricky regexps. If this were a problem I needed to solve, I'd do it like this:
for m in re.finditer("one.*?four", a):
if "two" not in m.group():
break
It's tricky enough that I'm using a minimal match there (.*?
). Regexps can be a real pain :-(
EDIT: LOL! But the messier regexp at the top fails yet again if you make the string harder still:
a = "one two three four five six one three four seven two four"
FINALLY: here's a correct solution:
>>> a = 'one two three four five six one three four seven two four'
>>> m = re.search("one([^t]|t(?!wo))*four", a)
>>> m.group()
'one three four'
>>> m.span()
(28, 42)
I know you said you wanted m.end()
to be 41, but that was incorrect.