Stop matching when performing a character sequence: fixing lookbehind

I have the following regexp:

(?P<question>.+(?<!\[\[))

It is intended to match hello world!in a stringhello world! [[A string typically used in programming examples]]

But I just match the entire string, and I can't figure out why. I tried all the search options, but that just won't work ...

Does anyone know how to fix this problem?

Thank you
CFP.

+3
source share
1 answer

You only check the turn at the end of the match, which means that he can match the string as much as possible, and then check the back. Since you don’t have at the end of the line [[, the match is successful.

, , , :

>>> s = 'hello world! [[A string typically used in programming examples]]'
>>> regex = re.compile('(?P<question>((?!\[\[).)+)')
>>> regex.match(s).group('question')
'hello world! '

, - s.find('[[') .

+3

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


All Articles