0

Is it possible to include the markers in a lookahead & lookbehind search?

Example:

str = "my cat is the best pet in the world"
re.findall('(?s)(?<=cat)(.*?)(?=pet)', str)

will return "is the best" what I want is "cat is the best pet"

thanks!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
B_Ch
  • 19
  • 1

1 Answers1

0

You should just search directly for \bcat.*?pet\b and forego with lookarounds:

str = "my cat is the best pet in the world"
m = re.findall(r'\bcat.*?pet\b', str)
print(m)

This prints:

['cat is the best pet']
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360