Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: python regex "negative lookahead assertions" problems (http://www.velocityreviews.com/forums/t706193-re-python-regex-negative-lookahead-assertions-problems.html)

MRAB 11-22-2009 04:32 PM

Re: python regex "negative lookahead assertions" problems
 
Tim Chase wrote:
>>>>> import re
>>>>> line='2009-11-22 12:15:441 lmqkjsfmlqshvquhsudfhqf qlsfh
>>>>> qsduidfhqlsiufh qlsiuf qldsfhqlsifhqlius dfh warning qlsfj lqshf
>>>>> lqsuhf lqksjfhqisudfh qiusdfhq iusfh'
>>>>> re.match('.*(?!warning)',line)

>> <_sre.SRE_Match object at 0xb75b1598>
>>
>> I would expect that this would NOT match as it's a negative lookahead
>> and warning is in the string.

>
> This first finds everything (".*") and then asserts that "warning"
> doesn't follow it, which is correct in your example. You may have to
> assert that "warning" doesn't exist at every point along the way:
>
> re.match(r'(?:(?!warning).)*',line)
>
> which will match up-to-but-not-including the "warning" text. If you
> don't want it at all, you'd have to also anchor the far end
>
> re.match(r'^(?:(?!warning).)*$',line)
>
> but in the 2nd case I'd just as soon invert the test:
>
> if 'warning' not in line:
> do_stuff()
>

The trick is to think what positive lookahead you'd need if you wanted
check whether 'warning' is present:

'(?=.*warning)'

and then negate it:

'(?!.*warning)'

giving you:

re.match(r'(?!.*warning)', line)


All times are GMT. The time now is 05:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.