开发者

Stop matching when meeting a sequence of chars: fixing a lookbehind

I have the following regexp:

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

It is designed to match hello world! in the string hello world! [[A string typically used in programming examples]]

Yet I just matches the whole string, and I can't figure out why. I've tried all 开发者_Go百科flavors of lookaround, but it just won't work...

Anyone knows how to fix this problem?

Thanks,

CFP.


You're only checking the lookaround at the end of the match, which means it can match as much as possible of the string first, and then afterwards check the lookaround. Since you don't have [[ at the end of the string, the match succeeds.

What you need to do if you really want to use regular expressions here is to check the lookaround for every character you add, like this:

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

But note that it would be much easier just to use something like s.find('[[') instead of regular expressions here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜