Possible Duplicate:
Remove text inside brackets, parens, and/or braces
I would like to replace the parenthesis and the text between parenthesis in a large text file.
Example input (content in the text file):
Keep me (Remove Me 1). Again keep me (Remove Me 2). Again again keep me (Remove Me 3).
Output (content in a new text file):
Keep me. Again keep me. Again again keep me.
Is it possible to do this in R (say using grep)?
Yes, use gsub()
to replace all the text you don't want with an empty string.
x <- "Keep me (Remove Me 1). Again keep me (Remove Me 2). Again again keep me (Remove Me 3)."
Here is the regex you want:
gsub( " *\\(.*?\\) *", "", x)
[1] "Keep me. Again keep me. Again again keep me."
It works like this:
*?
finds 0 or more spaces before (and after) the parentheses.(
and )
are special symbols in a regex, you need to escape these, i.e. (\\(
.*?
is a wildcard find to find all characters, where the ?
means to find in a non-greedy way. This is necessary because regex is greedy by default. In other words, by default the regex will start the match at the first opening parentheses and ends the match at the last closing parentheses.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