regular expression for nth character in a string
On Apr 25, 2011, at 6:17 AM, Gon?alo Ferraz wrote:
Hi, I have a string "InTrouble" and want to extract, say, the first two characters: "In" or the last three: "blee" or the 3rd, 4th, and 5th: "Trou" Is there an easy way of doing this quickly with regular expressions in gsub, grep or similar?
Not greppish but seems to be the obvious approach:
> substr("Trouble", 1,2)
[1] "Tr"
> substr("Trouble", 3,5)
[1] "oub"
The greppish ways:
> sub("(^..)(.*$)", "\\1", "Troubles")
[1] "Tr"
> sub("(^..)(...)(.*$)", "\\2", "Troubles")
[1] "oub"
> sub("(^..)(.{3})(.*$)", "\\2", "Troubles")
[1] "oub"
David Winsemius, MD West Hartford, CT