[R] Logic with regexps
ggrothendieck at gmail.com
Sat Jun 12 16:10:13 CEST 2010
On Sat, Jun 12, 2010 at 5:38 AM, Ted Harding
<Ted.Harding at manchester.ac.uk> wrote:
> The following question has come up in an off-list discussion.
> Is it possible to construct a regular expression 'rex' out of
> two given regular expressions 'rex1' and 'rex2', such that a
> character string X matches 'rex' if and only if X matches 'rex1'
> AND X does not match 'rex2'?
> The desired end result can be achieved by logically combining
> the results of a grep using 'rex1' with the results of a grep
> on 'rex2', illustrated by the following example:
> ## Given character vector X (below), and two regular exdpressions
> ## rex1="abc", rex2="ijk", to return the elements of X which match
> ## rex1 AND do not match rex1:
> X <- c(
> "abcdefg", # Yes
> "abchijk", # No
> "mnopqrs", # No
> "ijkpqrs", # No
> "abcpqrs" ) # Yes
> rex1 <- "abc"
> rex2 <- "ijk"
> ix1<- grep(rex1,X)
> ix2<- grep(rex2,X)
> X[ix1[!(ix1 %in% ix2)]]
> ##  "abcdefg" "abcpqrs"
> Question: is there a way to construct 'rex' from 'rex1' and 'rex2'
> such that
> would given the same result?
rex <- "^(?!(.*ijk)).*abc"
grep(rex, X, perl = TRUE)
Also note that X[grep(rex, X, perl = TRUE)] can be written:
grep(rex, X, perl = TRUE, value = TRUE)
See ?regex for more info. Further regular expression links can be
found in the External Links box on the gsubfn home page at
More information about the R-help