[R] regex -> negate a word

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sun Jan 18 21:50:13 CET 2009


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




More information about the R-help mailing list