R 带子组的组的编号后引用
我有一个单词‘fan(s)’,当前面有一个代词-动词组合时,我想用fantac(s)来代替,如下所示R 带子组的组的编号后引用,r,regex,pcre,R,Regex,Pcre,我有一个单词‘fan(s)’,当前面有一个代词-动词组合时,我想用fantac(s)来代替,如下所示 gsub( "(((s?he( i|')s)|((you|they|we)( a|')re)|(I( a|')m)).{1,20})(\\b[Ff]an)(s?\\b)", '\\1\\2atic\\3', 'He\'s the bigest fan I know.', perl = TRUE, ignore.case = TRUE ) ## [1] "H
gsub(
"(((s?he( i|')s)|((you|they|we)( a|')re)|(I( a|')m)).{1,20})(\\b[Ff]an)(s?\\b)",
'\\1\\2atic\\3',
'He\'s the bigest fan I know.',
perl = TRUE, ignore.case = TRUE
)
## [1] "He's the bigest He'saticHe's I know."
我知道编号后的参考是指第一组的内括号。有没有办法让他们只引用外三个括号,其中三个组是:(fan之前的内容)(fan)(s\\b)
(伪代码)
我知道我的正则表达式可以替换所有的组,我知道它是有效的。这只是反向参考部分
gsub(
"(((s?he( i|')s)|((you|they|we)( a|')re)|(I( a|')m)).{1,20})(\\b[Ff]an)(s?\\b)",
'',
'He\'s the bigest fan I know.',
perl = TRUE, ignore.case = TRUE
)
## [1] " I know."
期望输出:
## [1] "He's the bigest fanatic I know."
比赛的例子
inputs我知道您在捕获组数量过多方面遇到了问题。将您不感兴趣的内容转换为您感兴趣的内容,或删除那些完全多余的内容:
((?:s?he(?: i|')s|(?:you|they|we)(?: a|')re|I(?: a|')m).{1,20})\b(Fan)(s?)\b
见
请注意,[Ff]
可以转换为F
或F
,因为您正在使用ignore.case=TRUE
参数
:
输出:
[1] "He's the bigest fanatic I know."
[2] "I am a huge fanatic of his."
[3] "I know she has lots of fans in his club"
[4] "I was cold and turned on the fan"
[5] "An air conditioner is better than 2 fans at cooling."
谢谢…学习了一个关于regex的非常简单的新东西…非常感谢。
gsub(
"((?:s?he(?: i|')s|(?:you|they|we)(?: a|')re|I(?: a|')m).{1,20})\\b(fan)(s?)\\b",
'\\1\\2atic\\3',
inputs,
perl = TRUE, ignore.case = TRUE
)
[1] "He's the bigest fanatic I know."
[2] "I am a huge fanatic of his."
[3] "I know she has lots of fans in his club"
[4] "I was cold and turned on the fan"
[5] "An air conditioner is better than 2 fans at cooling."