Regex returns to period or beginning of line

I would like to combine the word and then get everything until the first event of the period or the beginning of the line.

For example, given this string and the search for the word "regex":

s = 'Do not match this. Or this. Or this either. I like regex. It is hard, but regex is also rewarding.'

He must return:

>> I like regex.
>> It is hard, but regex is also rewarding.

I'm trying to raise my head forward and look behind, but (it seems) you cannot easily look back until you press something, only if it is right next to your template. I can get close to this:

pattern = re.compile(r'(?:(?<=\.)|(?<=^))(.*?regex.*?\.)')

But this gives me the first period, then everything up to "regex":

>> Do not match this. Or this. Or this either. I like regex.  # no!
>> It is hard, but regex is also rewarding.                   # correct
+4
source share
1 answer

You do not need to use images for this. A negative character class is your best friend:

(?:[^\s.][^.]*)?regex[^.]*\.?

or

[^.]*regex[^.]*\.?

"regex" .

, - .

:

, . - (?:(?<=\.)|(?<=^)).*?regex "regex", - . , , .

, , : (?:(?<=\.)|(?<=^)) (?<![^.])

+5

Source: https://habr.com/ru/post/1681837/


All Articles