« Return to Thread: Regular Expressions

Re: Regular Expressions

by Richard Cotton :: Rate this Message:

Reply to Author | View in Thread

> S=c("World_is_beautiful", "one_two_three_four","My_book")

> I need to extract the last but one element of the strings. So, my
> output should look like:
 
> Ans=c("is","three","My")

> gsub() can do this...but wondering how do I give the regular
expression....

sapply(strsplit(S, "_"), function(x) x[length(x)-1])

You could use regular expressions, but I think it would only be
complicating things.

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}

______________________________________________
R-help@... 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.
Regards,
Richie.

Mathematical Sciences Unit
HSL

 « Return to Thread: Regular Expressions

LightInTheBox - Buy quality products at wholesale price