473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2118
On 2008-07-01, se******@spawar.navy.mil <se******@spawar.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.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
On 2008-07-01, seber...@spawar.navy.mil <seber...@spawar.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,instr):
print "%s match %s? %s" %
(restr,instr,bool(re.match(restr,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,brestr,instr):
print "%s contains %s but NOT %s? %s" % \
(instr,arestr,brestr,
bool(re.search(arestr,instr) and
not re.search(brestr,instr)))

test2(a,b,"AAA")
test2(a,b,"BBB")
test2(a,b,"AAABBB")
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*************************@python.org] On Behalf Of
se******@spawar.navy.mil
Sent: Tuesday, July 01, 2008 2:29 AM
To: py*********@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
Jul 1 '08 #4

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

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

First_half_of_Regex#1(not
Regex#2)(any_char_Not_followed_by_Regex#2)*Second_ half_of_Regex#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', 'fooAAAnotBBBbar', 'fooAAAnotbar',
'foonotBBBbar', 'foonotbar')

for s in ss:
print s,
m = re.match(r'(foo(?!not)(?:.(?!not))*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
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...
1
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
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
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"....
7
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...
16
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)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.