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 ')''
To remove parentheses from string using Python, the easiest way is to use the Python sub() function from the re module. If your parentheses are on the beginning and end of your string, you can also use the strip() function.
Parentheses can be removed by multiplying the outside factor to each term inside the parentheses. Note: A negative sign outside parentheses can be understood as the coefficient -1. The distributive property may then be applied to remove the parentheses.
Escape the parenthesis with a double-backslash:
gsub("log\\(", "", string)
(Obligatory: http://xkcd.com/234/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With