Here are two ways to do this:
One: Find the desired string and replace the entire string with the bit that was found. (Known as trackback)
gsub(".*\\((.*)\\).*", "\\1", x)
[1] "Yay, string!"
This works because:
- You use the backlink
\\1to refer to the matched string in parentheses(.*) - ,
\\( \\).
: , , :
gsub(".*\\(|\\).*", "", x)
[1] "Yay, string!"
, | OR.