Reputation: 35
I really suck with regex so apologies in advance.
I know that if I do
StringFeat = "I am a feat with (feat including feat feet))"
removefeatTest = stringr::str_replace_all(StringFeat, "\\(.*?\\)", "")
this produces "I am a feat with )". I would like it to be "I am a feat with " instead.
Another example input string is "(feat. The D.O.C., Dr. Rock, Fresh K (Fila Fresh Crew)) blah blah blah", I would like the output to be " blah blah blah".
At the same time, hopefully this regex can also handle parentheses without nested parentheses, like "(feat. The D.O.C.)". But, I would like to specifically look out for "(feat" so that another parentheses like "(this" should not be triggered by the regex.
Is this feasible with regex? Thank you in advance.
Upvotes: 0
Views: 33
Reputation: 1255
Here you have 2 closing brackets. The code you are using will remove only one. You can use something like:
removefeatTest = stringr::str_replace_all(StringFeat, "\\(.
*?\\)+", "")
which also works with the second example you mentioned:
StringFeat = "(feat. The D.O.C., Dr. Rock, Fresh K (Fila Fresh Crew)) blah blah blah"
stringr::str_replace_all(StringFeat, "\\(.*?\\)+", "")
[1] " blah blah blah"
and you can target expression starting with "(feat" using:
stringr::str_replace_all(StringFeat, "^\\(feat.*?\\)+", "")
[1] " blah blah blah"
Upvotes: 1