[R] Logic with regexps

(Ted Harding) Ted.Harding at manchester.ac.uk
Sat Jun 12 17:47:00 CEST 2010


Thanks, Gabor, for the initiation to perly regexps! I've only
been used to extended ones till now. A pity, perhaps, that
"perl=TRUE" is not an option for the likes of browseEnv(),
help.search(), list.files() and ls() (which take extended regexps),
but one can always assign the output and then grep(...,perl=TRUE)
on that, as you illustrate.

It would seem that these "look-ahead" features cold allow quite
complex logical conditions to be built up (though with increasing
unreadability and head-scratching)!

Best wishes,
Ted.

On 12-Jun-10 14:10:13, Gabor Grothendieck wrote:
> On Sat, Jun 12, 2010 at 5:38 AM, Ted Harding
> <Ted.Harding at manchester.ac.uk> wrote:
>> 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?
> 
> Try this:
> 
>    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
> http://gsubfn.googlecode.com
> 
> ______________________________________________
> 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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 12-Jun-10                                       Time: 16:46:56
------------------------------ XFMail ------------------------------



More information about the R-help mailing list