[R] regular expression help

David Winsemius dwinsemius at comcast.net
Fri Jun 27 07:12:05 CEST 2014


On Jun 26, 2014, at 6:11 PM, C Lin wrote:

> Hi Duncan,
> 
> Thanks for trying to help. Sorry for not being clear.
> The string I'd like to get is 'AARSD1'
> It can be followed or preceded by white space or // or nothing
> 
> so, from test <- c('AARSD11','AARSD1-','AARSD1//','AARSD1 //','//AARSD1','AARSD1');
> 
> I want to match only 'AARSD1//','AARSD1 //','//AARSD1','AARSD1'

Perhaps you want jsut 

grepl('^AARSD1//$|^AARSD1 //$|^//AARSD1$|^AARSD1', test)

> grepl('^AARSD1//$|^AARSD1 //$|^//AARSD1$|^AARSD1$', test)
[1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE

-- 
David.

> 

> Thanks,
> Lin
>  
> ----------------------------------------
>> From: dulcalma at bigpond.com
>> To: baccts at hotmail.com; r-help at r-project.org
>> Subject: RE: [R] regular expression help
>> Date: Fri, 27 Jun 2014 10:59:29 +1000
>> 
>> Hi
>> 
>> You only have a vector of length 5 and I am not quite sure of the string you
>> are testing
>> so try this
>> 
>> grep('[/]*\\<AARSD1\\>[/]*',test)
>> 
>> Duncan
>> 
>> Duncan Mackay
>> Department of Agronomy and Soil Science
>> University of New England
>> Armidale NSW 2351
>> Email: home: mackay at northnet.com.au
>> 
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
>> Behalf Of C Lin
>> Sent: Friday, 27 June 2014 10:05
>> To: r-help at r-project.org
>> Subject: [R] regular expression help
>> 
>> Dear R users,
>> 
>> I need to match a string. It can be followed or preceded by whitespace or //
>> or nothing.
>> How do I code it in R?
>> 
>> For example:
>> test <- c('AARSD11','AARSD1-','AARSD1//','AARSD1 //','//AARSD1');
>> grep('AARSD1(\\s*//*)',test);
>> 
>> should return 3,4,5 and 6.
>> 
> 


David Winsemius
Alameda, CA, USA



More information about the R-help mailing list