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"
> gsub("(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)", "\\5, \\7, \\9, \\10, \\15", test, perl = TRUE)
[1] "e, g, i, a0, a5"
See Regular Expressions with The R Language:
You can use the backreferences
\1
through\9
in the replacement text to reinsert text matched by a capturing group. There is no replacement text token for the overall match. Place the entire regex in a capturing group and then use\1
.
But with PCRE you should be able to use named groups. So try (?P<
name
>
regex
)
for groupd naming and (?P=
name
)
as backreference.
The stri_replace_*_regex
functions from the stringi package do not have such limitations:
library("stringi")
stri_replace_all_regex("abcdefghijkl", "(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)", "$10$1$11$12")
## [1] "jakl"
If you want to follow the 1st capture group with 1, use e.g.
stri_replace_all_regex("abcdefghijkl", "(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)", "$10$1$1\\1$12")
## [1] "jaa1l"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With