Let's say I have a text:
cool randomtext1 cool randomtext2 cool randomtext3 good cool
How would I match the most inner
cool randomtext3 good
When I try
r'cool.*?good'It matches
cool randomtext1 cool randomtext2 cool randomtext3 good
Let's say I have a text:
cool randomtext1 cool randomtext2 cool randomtext3 good cool
How would I match the most inner
cool randomtext3 good
When I try
r'cool.*?good'It matches
cool randomtext1 cool randomtext2 cool randomtext3 good
You can use this look-around based regex:
r'\bcool\b(?:(?!\b(cool|good)\b).)*\bgood\b'
Here (?:(?!\b(cool|good)\b).)*
will match 0 or more of any text that is not cool
or good
. \b
has been used for word boundaries.
This will match cool randomtext3 good
.
Code:
p = re.compile(ur'\bcool\b(?:(?!\b(cool|good)\b).)*\bgood\b')
test_str = u"cool randomtext1 cool randomtext2 cool randomtext3 good cool"
re.findall(p, test_str)