mike
mike

Reputation: 23811

Remove parenthesis from a character string

I am trying to remove a parenthesis from a string in R and run into the following error:

string <- "log(M)"
gsub("log", "", string) # Works just fine
gsub("log(", "", string) #breaks
# Error in gsub("log(", "", test) : 
#   invalid regular expression 'log(', reason 'Missing ')''

Upvotes: 49

Views: 65229

Answers (3)

glenninboston
glenninboston

Reputation: 1045

If you are not a regex specialist (many of us are not!), I find it more straight forward to separate the removal of the unneeded text and the parens, provided your query supports that.

The question seems to indicate only wanting to remove parens, so you could use:

gsub(paste(c("[(]", "[)]"), collapse = "|"), "", string)

This results in the string without parens: "logM"

If you also want to remoeve the "M"

gsub(paste(c("M", "[(]", "[)]"), collapse = "|"), "", string)

This results in the result "log"

Upvotes: 1

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162321

Ben's answer gives you the good generally applicable way of doing this.

Alternatively, in your situation you could use the fixed=TRUE argument, like this:

gsub("log(", "", string, fixed=TRUE)
# [1] "M)"

It is appropriate whenever the pattern argument to gsub() is a character string containing the literal sequence of characters you are searching for. Then, it's nice because it allows you to type the exact pattern that you are searching for, without escapes etc.

Upvotes: 28

Ben Bolker
Ben Bolker

Reputation: 226097

Escape the parenthesis with a double-backslash:

gsub("log\\(", "", string)

(Obligatory: http://xkcd.com/234/)

Upvotes: 76

Related Questions