1

I'm trying to write a regex in Python that will match a single sentence (from . to .) containing a specific word. The regex I wrote is r"\..*?hello.*?\.". As you can see I'm using the non-greedy quantifier. The text I'm matching against is:

This is the 1st sentence. This is the 2nd. This is the 3rd, hello, world. This is the 4th.

(here's a link to regex101)

I'd like the match to be This is the 3rd, hello, world. , but though the *? quantifier is non-greedy, the match includes also the 2nd sentence. What am I missing?

Gino
  • 675
  • 2
  • 10
  • 20

0 Answers0