[R] regex -> negate a word

jim holtman jholtman at gmail.com
Sun Jan 18 20:17:38 CET 2009


Just remove those elements that match:

> x <- c("abcdef", "defabc", "qwerty")
> x[-grep('abc',x)]
[1] "qwerty"
>


On Sun, Jan 18, 2009 at 1:35 PM, Rau, Roland <Rau at demogr.mpg.de> wrote:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list