0

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
airnet
  • 2,565
  • 5
  • 31
  • 35

1 Answers1

1

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.

RegEx Demo

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)
anubhava
  • 761,203
  • 64
  • 569
  • 643