When writing regular expressions with R's gsub()
function I thought that I could use capture groups by enclosing patterns in (...)
, and referring to the captured patterns with \\1
to "grab" them.
However, that doesn't appear to work in this example:
> gsub("([^-]+$)", "\\1", "xxx-yyy-zzz-abc")
[1] "xxx-yyy-zzz-abc"
The actual output is xxx-yyy-zzz-abc
when I expected it to be abc
. The regular expression above ([^-]+$)
captures the string after the last hyphen -
, and I confirmed it here with this regex demo.
Why isn't my output abc
?
If I instead remove my captured pattern from the original string, everything works as expected, and abc
is removed from original string.
> gsub("([^-]+$)", "", "xxx-yyy-zzz-abc")
[1] "xxx-yyy-zzz-"
What do I populate the code with below to get the output abc
? And what went wrong?
> gsub("______", "______", "xxx-yyy-zzz-abc")
[1] "abc"