[R] help with regular expressions in R

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


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

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

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

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

More information about the R-help mailing list