or with sub
from base R
:
sub("[^(]+\\(([^)]+)\\).*", "\\1", "A B C (123-456-789)")
#[1] "123-456-789"
Explanation:
[^(]+
: matches anything except an opening bracket
\\(
: matches an opening bracket, which is just before what you want
([^)]+)
: matches the pattern you want to capture (which is then retrieved in replacement="\\1"
), which is anything except a closing bracket
\\).*
matches a closing bracket followed by anything, 0 or more times
Another option with look-ahead and look-behind
sub(".*(?<=\\()(.+)(?=\\)).*", "\\1", "A B C (123-456-789)", perl=TRUE)
#[1] "123-456-789"