[R] regex -> negate a word

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sun Jan 18 22:04:26 CET 2009


Wacek Kusnierczyk wrote:
>
>> 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.
>   

incidentally, recent pcre accepts such regexes:

# r code
ungrep = function(pattern, x, ...)
    grep(paste(pattern, "(*COMMIT)(*FAIL)|(*ACCEPT)", sep=""), x,
perl=TRUE, ...)

strings = c("abc", "xyz")
pattern = "a[a-z]"
(filtered = strings[ungrep(pattern, strings)])
# "xyz"

vQ




More information about the R-help mailing list