views:

50

answers:

1

Hello everyone!

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 flavors of lookaround, but it just won't work...

Anyone knows how to fix this problem?

Thanks,
CFP.

+2  A: 

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.

Mark Byers
Great, thanks! In fact, this regexp is actually a small part of a larger template, which justifies using regexps.
CFP