[R] Hep with regex! - combination of ^, |, \\, _ and $

David Winsemius dwinsemius at comcast.net
Fri Sep 18 00:08:59 CEST 2015


On Sep 17, 2015, at 2:11 PM, Dimitri Liakhovitski wrote:

> (x <- c("q10_1", "q10_2", "q10_11", "q12_1", "q12_2", "q13_1", "q13_11"))
> 
> # Which strings start with "q10" or "q12? - WORKS
> x[grep("^q10|q12", x)]
> 
> # Which strings end with "1"? - WORKS
> x[grep("1$", x)]
> 
> # Which strings end with "_1"? - WORKS
> x[grep("\\_1$", x)]
> 
> # Which strings start with "q10" AND contain a "1"? - WORKS
> x[grep("^q10.+1", x)]
> 
> # Which strings start with "q10" AND end with a "_1"? - DOES NOT WORK
> x[grep("^q10.+\\_1$", x)]
> 
> # Which strings start with "q10" or "q12 AND end with "_1"? - WORKS INCORRECTLY
> x[grep("^q10|q12.+\\_1$", x)]
> 

You solved the last one with grouping around the OR-operator. The penultimate error needs only a very slight change to find a single instance in x:

x[grep("^q10.*_1$", x)]
[1] "q10_1"

Using "+" requires at leas one character between the two end, whereas you use the "*" operator to allow for the possibility of no intervening characters.

-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list