[R] regex -> negate a word

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


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

this was a toy example, but if you need this sort of ungrep with
patterns involving alterations, you need a fix:

ungrep("a|x", strings, value=TRUE)
# "abc"
# NOT character(0)

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

ungrep("a|x", strings, value=TRUE)
# character(0)


vQ




More information about the R-help mailing list