Skip to content
Back to formatted view

Raw Message

Message-ID: <49739605.90207@idi.ntnu.no>
Date: 2009-01-18T20:50:13Z
From: Wacek Kusnierczyk
Subject: regex -> negate a word
In-Reply-To: <971536df0901181154s66d1e3abg1750979c0f4716b1@mail.gmail.com>

Gabor Grothendieck wrote:
> Try this:
>
> grep("^([^a]|a[^b]|ab[^c])*.{0,2}$", x, perl = TRUE)
>
>   
... and see how cumbersome it becomes for a pattern as trivial as 'abc'. 

in perl, you typically don't invent such negative patterns, but rather
"don't match" positive patterns: instead of the match operator =~ and a
negative pattern, you use the no-match operator !~ and a positive pattern:

@strings = ("abc", "xyz");
@filtered = grep $_ !~ /abc/, @strings;

in r, one way to do the no-match is using -grep, but taking care of the
special case of no matches at all in the input vector.


> On Sun, Jan 18, 2009 at 2:37 PM, Rau, Roland <Rau at demogr.mpg.de> wrote:
>   
>> Thank you very much to all of you for your fast and excellent help.
>> Since the "-grep(...)" solution seems to be favored by most of the answers,
>> I just wonder if there is really no regular expression which does the job?!?
>>     

in perl 5.10, you can try this:

@strings = ("abc", "xyz");
@filtered = grep $_ =~ /(abc)(*COMMIT)(*FAIL)|(*ACCEPT)/, @strings;

which works by making a string that matches the pattern fail, and any
other string succeed despite no match.

vQ