How to use gsub with more than 9 backreferences? I would expect the output in the example below to be \"e, g, i, j, o\".
> test <- \"abcdefghijklmnop\"
&g
This limitation to 9 backreferences is specific to the sub()
and gsub()
functions, not to functions like grep()
and the like. Support for more than 9 backreferences in R implies using PCRE regular expression (i.e. the perl=TRUE
argument); however, even with this option, the sub() and gsub() functions do not support it.
The R documentation is explicit on this point: see ?regexp
There can be more than 9 backreferences (but the replacement in sub can
only refer to the first 9).
Furthermore the idea of using named capture groups to circumvent this limitation is bound to fail since named capture groups are not supported with sub() functions.
regexpr and gregexpr support ‘named capture’. If groups are named,
e.g., "(?[A-Z][a-z]+)" then the positions of the matches are also
returned by name. (Named backreferences are not supported by sub.)