473,498 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regular expression negate a word (not character)


somebody who is a regular expression guru... how do you negate a word
and grep for all words that is

tire

but not

snow tire

or

snowtire

so for example, it will grep for

winter tire
tire
retire
tired

but will not grep for

snow tire
snow tire
some snowtires

need to do it in one regular expression

Jan 26 '08 #1
8 18248
On Jan 25, 5:16 pm, Summercool <Summercooln...@gmail.comwrote:
somebody who is a regular expression guru... how do you negate a word
and grep for all words that is

tire

but not

snow tire

or

snowtire
i could think of something like

/[^s][^n][^o][^w]\s*tire/i

but what if it is not snow but some 20 character-word, then do we need
to do it 20 times to negate it? any shorter way?

Jan 26 '08 #2
[newsgroups line fixed, f'ups set to clpm]

Quoth Summercool <Su************@gmail.com>:
On Jan 25, 5:16 pm, Summercool <Summercooln...@gmail.comwrote:
somebody who is a regular expression guru... how do you negate a word
and grep for all words that is

tire

but not

snow tire

or

snowtire

i could think of something like

/[^s][^n][^o][^w]\s*tire/i

but what if it is not snow but some 20 character-word, then do we need
to do it 20 times to negate it? any shorter way?
This is no good, since 'snoo tire' fails to match even though you want
it to. You need something more like

/ (?: [^s]... | [^n].. | [^o]. | [^w] | ^ ) \s* tire /ix

but that gets *really* tedious for long strings, unless you generate it.

Ben

Jan 26 '08 #3
to add to the test cases, the regular expression must be able to grep
snowbird tire
tired on a snow day
snow tire and regular tire
Jan 26 '08 #4
Summercool:
to add to the test cases, the regular expression must be able to grep
snow tire and regular tire
I presume there only the second tire has to be found.

This is my first try:

text = """
tire
word tire word
word retire word
word tired word
snowbird tire word
tired on a snow day word
snow tire and regular tire word
word snow tire word
word snow tire word
word some snowtires word
"""

import re

def finder(text):
patt = re.compile( r"\b (\w*) \s* (tire)", re.VERBOSE)
for mo in patt.finditer(text):
if not mo.group(1).endswith("snow"):
yield mo.start(2)

for end in finder(text):
print end

The (lazy) output is the starting point of the "tire" that match:
1
11
28
43
63
73
120

Bye,
bearophile
Jan 26 '08 #5
On Jan 26, 1:16 am, Summercool <Summercooln...@gmail.comwrote:
somebody who is a regular expression guru... how do you negate a word
and grep for all words that is

tire

but not

snow tire

or

snowtire

so for example, it will grep for

winter tire
tire
retire
tired

but will not grep for

snow tire
snow tire
some snowtires

need to do it in one regular expression
Try the answer here:
http://mail.python.org/pipermail/tut...st/024902.html
Jan 26 '08 #6
Paddy:
Try the answer here:
http://mail.python.org/pipermail/tut...st/024902.html
But in the OP problem there can be variable-sized spaces in the
middle...

Bye,
bearophile
Jan 26 '08 #7
[A complimentary Cc of this posting was sent to
Summercool
<Su************@gmail.com>], who wrote in article <27**********************************@n20g2000hsh. googlegroups.com>:
so for example, it will grep for

winter tire
tire
retire
tired

but will not grep for

snow tire
snow tire
some snowtires
This does not describe the problem completely. What about

thisnow tire
snow; tire

etc? Anyway, one of the obvious modifications of

(^ | \b(?!snow) \w+ ) \W* tire

should work.

Hope this helps,
Ilya

Jan 26 '08 #8
On Jan 25, 7:16*pm, Summercool <Summercooln...@gmail.comwrote:
somebody who is a regular expression guru... how do you negate a word
and grep for all words that is

* tire

but not

* snow tire

or

* snowtire
Too bad pyparsing's not an option. Here's what it would look like:

data = """
Match:
winter tire
tire
retire
tired
But not match:
snow tire
snow tire
some snowtires
snowbird tire
tired on a snow day
snow tire and regular tire

"""

from pyparsing import CaselessLiteral,Literal,line

# caseless wasn't really necessary but you never know
# when you'll run into a "Snow tire"
snow = CaselessLiteral("snow")
tire = Literal("tire")
tire.ignore(snow + tire)

for matchTokens,matchStart,matchEnd in tire.scanString(data):
print line(matchStart, data)
Prints:
winter tire
tire
retire
tired
snowbird tire
tired on a snow day
snow tire and regular tire

-- Paul
Jan 28 '08 #9

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

Similar topics

1
4154
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
8221
by: Neil Morris | last post by:
Hi I have the following code that lists first names of people who's surnames are 'Morris'. What I am trying to do is search for first names that don't start at the beginning of the line ie. have...
0
383
by: Dave | last post by:
I need to find a way to do a replace based on the following logic: Any substring which: 1) begins with "/" a) but not "{/" 2) is not followed by any of the following strings "cf" "line" 3)...
3
2131
by: Ryan Taylor | last post by:
Hello. I am trying to create a regular expression that will let me know if a string has the following criteria. Order does not matter in the string, but when building a regular expression it...
6
2270
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
3
2547
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
5128
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
1895
by: Summercool | last post by:
somebody who is a regular expression guru... how do you negate a word and grep for all words that is tire but not snow tire or
1
1565
by: AAaron123 | last post by:
I found this on the Internet and tried a few of them and they worked in VS2008. Actually it was in a different form but I converted to make a smaller file. The data is the same as the original. ...
0
7002
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
7205
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...
1
6887
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...
0
7379
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...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
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...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.