[R] Extract part of string

David Winsemius dwinsemius at comcast.net
Sun Jan 5 18:31:03 CET 2014


On Jan 5, 2014, at 9:24 AM, David Winsemius wrote:

> 
> On Jan 5, 2014, at 12:45 AM, Ron Michael wrote:
> 
>> Hi,
>> 
>> I am struggling to extract a part of string using regular expression in R.
>> 
>> Let say, the string to be extracted is 'ron' from the expression 'my name is ron'. So I tried following:
>> 
>>> gsub("[^(ron)]", "", 'my name is ron')
>> [1] "nron"
>> 
>> 
>> I was expecting to get 'ron', which is not the case.
> 
> The "[" operator in regex does not take into account the sequence of the target letters so you get all "r", all"o", and all "n" in the order in which they occur.  "[onr]" will have the same meaning as "[ron]".
> 

Furthermore, the "(" and ")" would have been interpreted literally since those characters are not special inside character classes.

> gsub("[^(ron)]", "", 'my () name is ron')
[1] "()nron"


> Try:
> 
>> gsub("^.*(ron).*$", "\\1", 'my name is ron')
> [1] "ron"
> 
> -- 
> David Winsemius
> Alameda, CA, USA
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA




More information about the R-help mailing list