It is not simple to negate a regular expression. This obviates the need takes the long way round: negating the corresponding grepl call.

ngrep(pattern, x, value = FALSE, ...)

Arguments

x, value, pattern

As in grep.

...

Arguments passed to grepl.

Value

If value is FALSE (the default), indices of x which do not match the pattern; if TRUE, the values of x themselves.

Examples

grep("[a-h]", letters)
#> [1] 1 2 3 4 5 6 7 8
ngrep("[a-h]", letters)
#> [1] 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
txt <- c("The", "licenses", "for", "most", "software", "are", "designed", "to", "take", "away", "your", "freedom", "to", "share", "and", "change", "it.", "", "By", "contrast,", "the", "GNU", "General", "Public", "License", "is", "intended", "to", "guarantee", "your", "freedom", "to", "share", "and", "change", "free", "software", "--", "to", "make", "sure", "the", "software", "is", "free", "for", "all", "its", "users") grep("[gu]", txt, value = TRUE)
#> [1] "designed" "your" "change" "Public" "guarantee" "your" #> [7] "change" "sure" "users"
ngrep("[gu]", txt, value = TRUE)
#> [1] "The" "licenses" "for" "most" "software" "are" #> [7] "to" "take" "away" "freedom" "to" "share" #> [13] "and" "it." "" "By" "contrast," "the" #> [19] "GNU" "General" "License" "is" "intended" "to" #> [25] "freedom" "to" "share" "and" "free" "software" #> [31] "--" "to" "make" "the" "software" "is" #> [37] "free" "for" "all" "its"