[R] help with regular expressions in R

Chuck Taylor ctaylor at tibco.com
Thu Aug 20 17:46:37 CEST 2009


Mark,

Try this:

> myCharVec
[1] "[the rain in spain]" "(the rain in spain)"

> gsub("\\[.*\\]", "", myCharVec)
[1] ""                    "(the rain in spain)"

You need two backslashes to "escape" the square brackets. The regular
expression "\\[.\\]" translates to "a [ followed by 0 or more instances
of any character followd by ]".

Best regards,
Chuck Taylor
TIBCO Spotfire
Seattle

-----Original Message-----

I want to eliminate all characters that fall within square brackets as
well
as the brackets themselves, returning an "". ...

#what I want
[1] ""   "(the rain in spain)"




More information about the R-help mailing list