regular expression negate a word (not character)
Ben Morrow
ben at morrow.me.uk
Fri Jan 25 22:37:53 EST 2008
[newsgroups line fixed, f'ups set to clpm]
Quoth Summercool <Summercoolness at gmail.com>:
> On Jan 25, 5:16 pm, Summercool <Summercooln... at gmail.com> wrote:
> > 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
More information about the Python-list
mailing list