Velocity Reviews

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

Jelle Smet 11-22-2009 01:58 PM

python regex "negative lookahead assertions" problems
 
Hi List,

I'm trying to match lines in python using the re module.
The end goal is to have a regex which enables me to skip lines which have ok and warning in it.
But for some reason I can't get negative lookaheads working, the way it's explained in "http://docs.python.org/library/re.html".

Consider this example:

Python 2.6.4 (r264:75706, Nov 2 2009, 14:38:03)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 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.


Thanks,


--
Jelle Smet
http://www.smetj.net

Helmut Jarausch 11-22-2009 03:05 PM

Re: python regex "negative lookahead assertions" problems
 
On 11/22/09 14:58, Jelle Smet wrote:
> Hi List,
>
> I'm trying to match lines in python using the re module.
> The end goal is to have a regex which enables me to skip lines which have ok and warning in it.
> But for some reason I can't get negative lookaheads working, the way it's explained in "http://docs.python.org/library/re.html".
>
> Consider this example:
>
> Python 2.6.4 (r264:75706, Nov 2 2009, 14:38:03)
> [GCC 4.4.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> 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.
>


'.*' eats all of line. Now, when at end of line, there is no 'warning' anymore, so it matches.
What are you trying to achieve?

If you just want to single out lines with 'ok' or warning in it, why not just
if re.search('(ok|warning)') : call_skip

Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

Helmut Jarausch 11-23-2009 09:20 AM

Re: python regex "negative lookahead assertions" problems
 
On 11/22/09 16:05, Helmut Jarausch wrote:
> On 11/22/09 14:58, Jelle Smet wrote:
>> Hi List,
>>
>> I'm trying to match lines in python using the re module.
>> The end goal is to have a regex which enables me to skip lines which
>> have ok and warning in it.
>> But for some reason I can't get negative lookaheads working, the way
>> it's explained in "http://docs.python.org/library/re.html".
>>
>> Consider this example:
>>
>> Python 2.6.4 (r264:75706, Nov 2 2009, 14:38:03)
>> [GCC 4.4.1] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> 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.
>>

>
> '.*' eats all of line. Now, when at end of line, there is no 'warning'
> anymore, so it matches.
> What are you trying to achieve?
>
> If you just want to single out lines with 'ok' or warning in it, why not
> just
> if re.search('(ok|warning)') : call_skip
>


Probably you don't want words like 'joke' to match 'ok'.
So, a better regex is

if re.search('\b(ok|warning)\b',line) : SKIP_ME

Helmut.



--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany


All times are GMT. The time now is 06:51 AM.

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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57