[R] Logic with regexps

From: Ted Harding <Ted.Harding_at_manchester.ac.uk>
Date: Sat, 12 Jun 2010 10:38:53 +0100 (BST)


Greetings,
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)]]
## [1] "abcdefg" "abcpqrs"

Question: is there a way to construct 'rex' from 'rex1' and 'rex2' such that

  X[grep(rex,X)]

would given the same result?

I've not managed to find anything helpful in desciptions of regular expression syntax, though one feels it should be possible if this is capable of supporting a logically complete language!

With thanks,
Ted.



E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 12-Jun-10                                       Time: 10:38:45
------------------------------ XFMail ------------------------------

______________________________________________
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. Received on Sat 12 Jun 2010 - 09:42:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 12 Jun 2010 - 14:20:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive