gpt4 book ai didi

regex - gsub() 中有超过 9 个反向引用

转载 作者:行者123 更新时间:2023-12-04 01:48:15 25 4
gpt4 key购买 nike

如何使用具有 9 个以上反向引用的 gsub?
我希望下面示例中的输出为“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"

最佳答案

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.



但是使用 PCRE 你应该可以使用 named groups .所以试试 (?P< name > regex )用于分组命名和 (?P= name )作为反向引用。

关于regex - gsub() 中有超过 9 个反向引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1400937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com