Click here to get back home

regular expression negate a word (not character)

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
regular expression negate a word (not character) Summercool 01-25-2008
Posted by Summercool on January 25, 2008, 8:16 pm
Please log in for more thread options

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


Posted by Summercool on January 25, 2008, 9:15 pm
Please log in for more thread options
> 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?


Posted by Ben Morrow on January 25, 2008, 10:37 pm
Please log in for more thread options
[newsgroups line fixed, f'ups set to clpm]

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


Posted by paulaireilly on January 25, 2008, 9:42 pm
Please log in for more thread options
> 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
> snow tire
> some snowtires
>
> need to do it in one regular expression

You might be looking for a <b>negative lookahead assertion</b>. Look
that up in a handy
source. The syntax is approximately


(?!foo) --> will match at any place "betwee" chars not immediately
preceded by a"foo".

Now, you have to add in the "bar" afterwards. But remember that

(?!foo) takes up zero width. And be careful about /.*/ matching
anything including zero
chars.

I would say more but this looks sorta like a homework assignment to
me. So this is a "hint" post. I or someone else could do a "solution"
post later, but just having the phrase
"negative lookahead assertion" to look up on the Web or in a book
index will probably
answer all your questions.

Note that what you *really* want is a "negative lookbehind assertion",
to put right in front of the "tire" in your example (or my "bar"), but
I think those won't be working until Perl 6.

Posted by Ben Morrow on January 25, 2008, 10:27 pm
Please log in for more thread options

>
> You might be looking for a <b>negative lookahead assertion</b>. Look
<snip>
>
> Note that what you *really* want is a "negative lookbehind assertion",
> to put right in front of the "tire" in your example (or my "bar"), but
> I think those won't be working until Perl 6.

No, they work perfectly well in Perl 5, at least for fixed-length
strings. Syntax is (?<= ) and (?<! ). In 5.10 you can get
variable-length positive (but not negative) lookbehind at the start of
the match using \K.

Ben


Similar ThreadsPosted
Regular expression question : how to not match a word January 26, 2006, 12:33 pm
Question about "?" character in Perl Regular Expression January 2, 2008, 2:58 am
Regular expression for matching words containing underscore _ character December 12, 2007, 10:27 am
need to negate regex in middle of expression June 20, 2005, 6:19 am
How to determine if a word has an extended character? May 20, 2008, 8:54 am
Non match word list with Regular Expressions August 18, 2004, 9:50 am
Re: Non match word list with Regular Expressions August 19, 2004, 4:12 am
Regular Expression help please October 4, 2004, 9:56 pm
Regular expression September 23, 2004, 11:53 am
Regular Expression for - \C\R... November 2, 2004, 11:15 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap