python regex "negative lookahead assertions" problems
python.list at tim.thechases.com
Sun Nov 22 15:52:39 CET 2009
>>>> import re
>>>> line='2009-11-22 12:15:441 lmqkjsfmlqshvquhsudfhqf qlsfh qsduidfhqlsiufh qlsiuf qldsfhqlsifhqlius dfh warning qlsfj lqshf lqsuhf lqksjfhqisudfh qiusdfhq iusfh'
> <_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:
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
but in the 2nd case I'd just as soon invert the test:
if 'warning' not in line:
More information about the Python-list