[R] Extract part of string

arun smartpink111 at yahoo.com
Sun Jan 5 18:13:00 CET 2014

You could try:
sub(".*(ron)", "\\1", 'my name is ron')
#[1] "ron"

 sub('^.* ([[:alnum:]]+)$', '\\1','my name is ron')
#[1] "ron"

On Sunday, January 5, 2014 11:52 AM, Ron Michael <ron_michael70 at yahoo.com> wrote:

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.

Could someone please guide me what will be the right way for that?

Thanks for your help.

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list