Skip to content
Prev 167427 / 398502 Next

regex -> negate a word

Gabor Grothendieck wrote:
... 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.
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