[R] Regular Expressions

Richard.Cotton at hsl.gov.uk
Tue, 13 May 2008 10:02:24 -0000

> 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}}