473,570 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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" ?

Chris
Jul 1 '08 #1
4 2129
On 2008-07-01, se******@spawar .navy.mil <se******@spawa r.navy.milwrote :
I'm looking over the docs for the re module and can't find how to
"NOT" an entire regex.
(?! R)
How make regex that means "contains regex#1 but NOT regex#2" ?
(\1|(?!\2))

should do what you want.

Albert
Jul 1 '08 #2
On Jul 1, 2:34*am, "A.T.Hofkam p" <h...@se-162.se.wtb.tue. nlwrote:
On 2008-07-01, seber...@spawar .navy.mil <seber...@spawa r.navy.milwrote :
I'm looking over the docs for the re module and can't find how to
"NOT" an entire regex.

(?! R)
How make regex that means "contains regex#1 but NOT regex#2" ?

(\1|(?!\2))

should do what you want.

Albert
I think the OP wants both A AND not B, not A OR not B. If the OP want
to do re.match(A and not B), then I think this can be done as ((?!
\2)\1), but if he really wants CONTAINS A and not B, then I think this
requires 2 calls to re.search. See test code below:

import re

def test(restr,inst r):
print "%s match %s? %s" %
(restr,instr,bo ol(re.match(res tr,instr)))

a = "AAA"
b = "BBB"

aAndNotB = "(%s|(?!%s) )" % (a,b)

test(aAndNotB," AAA")
test(aAndNotB," BBB")
test(aAndNotB," AAABBB")
test(aAndNotB," zAAA")
test(aAndNotB," CCC")

aAndNotB = "((?!%s)%s) " % (b,a)

test(aAndNotB," AAA")
test(aAndNotB," BBB")
test(aAndNotB," AAABBB")
test(aAndNotB," zAAA")
test(aAndNotB," CCC")

def test2(arestr,br estr,instr):
print "%s contains %s but NOT %s? %s" % \
(instr,arestr,b restr,
bool(re.search( arestr,instr) and
not re.search(brest r,instr)))

test2(a,b,"AAA" )
test2(a,b,"BBB" )
test2(a,b,"AAAB BB")
test2(a,b,"zAAA ")
test2(a,b,"CCC" )

Prints:

(AAA|(?!BBB)) match AAA? True
(AAA|(?!BBB)) match BBB? False
(AAA|(?!BBB)) match AAABBB? True
(AAA|(?!BBB)) match zAAA? True
(AAA|(?!BBB)) match CCC? True
((?!BBB)AAA) match AAA? True
((?!BBB)AAA) match BBB? False
((?!BBB)AAA) match AAABBB? True
((?!BBB)AAA) match zAAA? False
((?!BBB)AAA) match CCC? False
AAA contains AAA but NOT BBB? True
BBB contains AAA but NOT BBB? False
AAABBB contains AAA but NOT BBB? False
zAAA contains AAA but NOT BBB? True
CCC contains AAA but NOT BBB? False
As we've all seen before, posters are not always the most precise when
describing whether they want match vs. search. Given that the OP used
the word "contains", I read that to mean "search". I'm not an RE pro
by any means, but I think the behavior that the OP wants is given in
the last 4 tests, and I don't know how to do that in a single RE.

-- Paul
Jul 1 '08 #3

-----Original Message-----
From: py************* *************** ****@python.org [mailto:python-
li************* ************@py thon.org] On Behalf Of
se******@spawar .navy.mil
Sent: Tuesday, July 01, 2008 2:29 AM
To: py*********@pyt hon.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 = 'fooAAAnotBBBba r'
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
Jul 1 '08 #4

-----Original Message-----
From: py************* *************** ****@python.org [mailto:python-
li************* ************@py thon.org] On Behalf Of Reedick, Andrew
Sent: Tuesday, July 01, 2008 10:07 AM
To: se******@spawar .navy.mil; py*********@pyt hon.org
Subject: RE: 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 = 'fooAAAnotBBBba r'
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

Fixed a bug with 'foonotbar'. Conceptually it breaks down into:

First_half_of_R egex#1(not
Regex#2)(any_ch ar_Not_followed _by_Regex#2)*Se cond_half_of_Re gex#1

However, if possible, I would make it a two pass regex. Match on
Regex#1, throw away any matches that then match on Regex#2. A two pass
is faster and easier to code and understand. Easy to understand == less
chance of a bug. If you're worried about performance, then a) a
complicated regex may or may not be faster than two simple regexes, and
b) if you're passing that much data through a regex, you're probably I/O
bound anyway.
import re

ss = ('foobar', 'fooAAABBBbar', 'fooAAAnotBBBba r', 'fooAAAnotbar',
'foonotBBBbar', 'foonotbar')

for s in ss:
print s,
m = re.match(r'(foo (?!not)(?:.(?!n ot))*bar)', s);
if m:
print m.groups()
else:
print
== output ==
foobar ('foobar',)
fooAAABBBbar ('fooAAABBBbar' ,)
fooAAAnotBBBbar
fooAAAnotbar
foonotBBBbar
foonotbar

*****

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
Jul 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1838
by: Flos | last post by:
Hello! I want to make a validation for a string which can be only letters AND space. for example "apple house", but not "apple3 house" i have found if (preg_match ("//", $string)) return false esle return true
1
149662
by: Robin Tucker | last post by:
Hi, I would like to select records from a table where the following criteria hold: SELECT * from Mytable where field x "contains" string @X or
5
2018
by: Mark Johnson | last post by:
Regex("@("); brings an error (missing ")"). How do you serarch for a ( with Regex ? Mark Johnson, Berlin Germany mj10777@mj10777.de
2
1306
by: Jeff Jarrell | last post by:
I want to use the regex.replace for a string containing "%s" I can't seem to get the "%s" escaped. I tried a normal "\%s" but that doesn't seem to do it. Picks up any "s". --------------------------------------------------------------------- Dim options As RegexOptions = RegexOptions.None Dim oRegex As Regex = New Regex("", options) ...
7
19406
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, I need to check if a textbox (of size = 1) contains a specific value (character). I could say something like If txt1.text.equals("X") or txt1.text.equals("Y")... then... Ideally, I would like to do something like Sql --If txt1.text In ("X", "Y", "Z") then...
16
2239
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string) pattern search functions. Where performance is an issue you can alway write your own specialized routine of course. However, for the occasional...
0
7944
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8149
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7699
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5523
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3681
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3671
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.