How make regex that means "contains regex#1 but NOT regex#2" ??

Reedick, Andrew jr9445 at ATT.COM
Tue Jul 1 10:06:42 EDT 2008



> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of
> seberino at spawar.navy.mil
> Sent: Tuesday, July 01, 2008 2:29 AM
> To: python-list at python.org
> Subject: How make regex that means "contains regex#1 but NOT regex#2"
> ??
> 
> I'm looking over the docs for the re module and can't find how to
> "NOT" an entire regex.
> 
> For example.....
> 
> How make regex that means "contains regex#1 but NOT regex#2" ?
> 

Match 'foo.*bar', except when 'not' appears between foo and bar.


import re

s = 'fooAAABBBbar'
print "Should match:", s
m = re.match(r'(foo(.(?!not))*bar)', s);
if m:
	print m.groups()

print

s = 'fooAAAnotBBBbar'
print "Should not match:", s
m = re.match(r'(foo(.(?!not))*bar)', s);
if m:
	print m.groups()


== Output ==
Should match: fooAAABBBbar
('fooAAABBBbar', 'B')

Should not match: fooAAAnotBBBbar



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621





More information about the Python-list mailing list