在R正则表达式中使用9个以上的反向引用 [英] Using more than nine back references in an R regex

查看:125
本文介绍了在R正则表达式中使用9个以上的反向引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码不起作用,因为\ 10,\ 11等的替换字符串无法正确读取.它将\ 10读为\ 1并打印0,您能帮我解决这个问题吗? 在其中一个线程中有一个答案,说我应该使用捕获或命名组,但是我真的不了解如何使用它们.

The code below does not work, because the replacement string for \10, \11, and so on, cannot be read properly. It reads \10 as \1 and print 0 instead, can you help me fix it? There is an answer in one of the threads, saying that I am supposed to use capturing or naming groups, but I don't really understand how to use them.

headline <- gsub("regexp with 10 () brackets",
"\\1 ### \\2 ### \\3 ### \\4 ### \\5 ### \\6 ### \\7 ### \\8 ### \\9 ###
\\10### \\11### \\12### \\13### \\14### \\15### \\16",
page[headline.index])

推荐答案

根据?regexp,自R-2.14.0起,命名捕获已在regexpr()gregexpr()中可用.不幸的是,它尚不可用可用于sub()或事实证明​​是gsub().因此,它可能对您仍然有用,但是可能需要比您期望的更多的腿部动作.

According to ?regexp, named capture has been available in regexpr() and gregexpr() since R-2.14.0. Unfortunately, it is not yet available for sub() or, it turns out, gsub(). So, it may still be useful to you, but will probably require a bit more legwork than you might have hoped.

(有关正在使用的命名组的一些示例,请参见?regexpr的示例部分.)

(For a few examples of naming groups in action, see the examples section of ?regexpr.)

稍后再添加,跟随GREG SNOW的答案

格雷格·斯诺(Greg Snow)暗示了使用gsubfn软件包进行此操作的可能性.以下示例显示gsubfn()确实可以处理九个以上的反向引用:

Greg Snow alluded to the possibility of doing this with the gsubfn package. Here's an example that shows that gsubfn() can indeed handle more than nine backreferences:

require(gsubfn)
string <- "1:2:3:4:5:6:7:8:9:10:11"
pat <- "^(\\d)+:(\\d)+:(\\d)+:(\\d)+:(\\d)+:(\\d)+:(\\d)+:(\\d)+:(\\d)+:(\\d)+:(\\d)+"
gsubfn(pat, ~ paste(a,b,c,d,e,f,g,h,i,j,k,j,i,h,g,f,e,d,c,e,a), string)  
# [1] "1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 5 1"

这篇关于在R正则表达式中使用9个以上的反向引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆