[R] regex -> negate a word

Rau, Roland Rau at demogr.mpg.de
Sun Jan 18 19:35:43 CET 2009


Dear all,

let's assume I have a vector of character strings:

x <- c("abcdef", "defabc", "qwerty")

What I would like to find is the following: all elements where the word
'abc' does not appear (i.e. 3 in this case of 'x').

Since I am not really experienced with regular expressions, I started
slowly and thought I find all word were 'abc' actually does appear:

> grep(pattern="abc", x=x)
[1] 1 2

So far, so good. Now I read that ^ is the negation operator. But it can
also denote the beginning of a string as in:

> grep(pattern="^abc", x=x)
[1] 1

Of course, we need to put it inside square brackets to negate the
expression [1]
> grep(pattern="[^abc]", x=x)
[1] 1 2 3

But this is not what I want either.

I'd appreciate any help. I assume this is rather easy and
straightforward.

Thanks,
Roland


[1] http://www.zytrax.com/tech/web/regex.htm: The ^ (circumflex or
caret) inside square brackets negates the expression....

----------
This mail has been sent through the MPI for Demographic Research.  Should you receive a mail that is apparently from a MPI user without this text displayed, then the address has most likely been faked. If you are uncertain about the validity of this message, please check the mail header or ask your system administrator for assistance.




More information about the R-help mailing list