Let’s say I have a string:
x <- "This is a string (Yay, string!)"
I’d like to parse the string and return “Yay, string!”
How do I do that?
I tried a bunch of grep/grepl/gsub/sub/etc but couldn’t find the right combination of regex or arguments. Sigh. I need to work on the regex skills.
Here are two ways of doing it:
One: Find the string you want, and replace the entire string with the bit that was found. (Known as back referencing)
This works because:
\\1to refer to the matched string in the parentheses(.*)\\(and\\).Two: Replace all the bits you don’t want with empty strings:
This works because the
|acts similar toOR.