I prefer to use gsub()
for this:
gsub(".*\\((.*)\\).*", "\\1", df)
[1] "id-1290"
The regex works like this:
- Find text inside the parentheses - not your real parentheses, but my extra set of parentheses, i.e.
(.*)
- Return this as a back-reference,
\\1
In other words, substitute all text in the string with the back reference
If you want to use regexp
rather than gsub
, then do this:
x <- regexpr("\\((.*)\\)", df)
x
[1] 11
attr(,"match.length")
[1] 9
attr(,"useBytes")
[1] TRUE
This returns a value of 11, i.e. the starting position of the found expression. And note the attribute match.length
that indicates how many characters were matched.
You can extract this with attr
:
attr(x, "match.length")
[1] 9
And then use substring
to extract the characters:
substring(df, x+1, x+attr(x, "match.length")-2)
[1] "id-1290"