Regular Expression question

Peter Otten __peter__ at web.de
Thu Oct 25 03:25:08 EDT 2007


looping wrote:

> On Oct 25, 8:49 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>>
>> needle = re.compile(r'create\s+or\s+replace\s+package(\s+body)?\s+',
>>                     re.IGNORECASE)
> 
> What I want here is a RE that return ONLY the line without the "body"
> keyword.
> Your RE return both.
> I know I could use it but I want to learn how to search something that
> is NOT in the string using RE.

You want a "negative lookahead assertion" then:

>>> import re
>>> s = """Isaac Newton
... Isaac Asimov
... Isaac Singer
... """
>>> re.compile("Isaac (?!Asimov).*").findall(s)
['Isaac Newton', 'Isaac Singer']

Peter



More information about the Python-list mailing list