More than 9 backreferences in gsub()

隐身守侯 提交于 2019-12-04 01:48:11

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.

Use strsplit instead:

test <- "abcdefghijklmnop"
strsplit(test, "")[[1]][c(5, 7, 9, 10, 15)]

It was my understanding that \10 would we understood as backreference 0 followed by a digit of 1. I think 9 is the max.

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"

According to this site, back references \10 to \99 works on some languages, but not most.

Those that are reported to work are

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., "(?<first>[A-Z][a-z]+)" then the positions of the matches are also
returned by name. (Named backreferences are not supported by sub.)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!