[R] regex -> negate a word

Eric Archer eric.archer at noaa.gov
Sun Jan 18 20:36:13 CET 2009


Roland,

I think you were almost there with your first example.  Howabout using:

 > x <- c("abcdef", "defabc", "qwerty")
 > y <- grep(pattern="abc", x=x)
 > z.char <- x[-y]
 > z.index <- (1:length(x))[-y]
 >
 > z.char
[1] "qwerty"
 > z.index
[1] 3

Cheers,
eric

Rau, Roland 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.

-- 

Eric Archer, Ph.D.
Southwest Fisheries Science Center
8604 La Jolla Shores Dr.
La Jolla, CA 92037
858-546-7121 (work)
858-546-7003 (FAX)

ETP Cetacean Assessment Program: http://swfsc.noaa.gov/prd-etp.aspx
Population ID Program: http://swfsc.noaa.gov/prd-popid.aspx



"Innocence about Science is the worst crime today."
    - Sir Charles Percy Snow

"Lighthouses are more helpful than churches."
    - Benjamin Franklin

    "...but I'll take a GPS over either one."
        - John C. "Craig" George




More information about the R-help mailing list