[英]Positive look ahead in R - passing variables
I got stuck in a regular expression. 我陷入了正常的表达。 I usually use this line of code to find overlapping repetitions in strings:
我通常使用这行代码来查找字符串中重叠的重复:
gregexpr("(?=ATGGGCT)",text,perl=TRUE)
[[1]]
[1] 16 45 52 75 203 210 266 273 327 364 436 443 480 506 534 570 649
attr(,"match.length")
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
attr(,"useBytes")
[1] TRUE
Now I want to give to gregexpr
a pattern contained in a variable: 现在我想给
gregexpr
一个包含在变量中的模式:
x="GGC"
and of course if I pass the variable x
, gregexpr
is going to search "x"
and not what the variable contains 当然,如果我传递变量
x
, gregexpr
将搜索"x"
而不是变量包含的内容
gregexpr("(?=x)",text,perl=TRUE)
[[1]]
[1] -1
attr(,"match.length")
[1] -1
attr(,"useBytes")
[1] TRUE
How can I pass my variable to gregexpr
in this case of positive look ahead? 在这种积极展望的情况下,如何将变量传递给
gregexpr
?
I'd play with the sprintf
function: 我会玩
sprintf
函数:
x <- "AGA"
text <- "ACAGAGACTTTAGATAGAGAAGA"
gregexpr(sprintf("(?=%s)", x), text, perl=TRUE)
## [[1]]
## [1] 3 5 12 16 18 21
## attr(,"match.length")
## [1] 0 0 0 0 0 0
## attr(,"useBytes")
## [1] TRUE
sprintf
substitutes the occurrence of %s
by the value of x
. sprintf
用x
的值替换%s
的出现。
You could use paste0
which is short for paste(x, sep="")
... 你可以使用
paste0
,它是paste(x, sep="")
缩写paste(x, sep="")
......
x <- "GGC"
text <- 'ATGGGCTATGGGCTATGGGCTATGGGCT'
gregexpr(paste0('(?=', x, ')'), text, perl=TRUE)
# [[1]]
# [1] 4 11 18 25
# attr(,"match.length")
# [1] 0 0 0 0
# attr(,"useBytes")
# [1] TRUE
And if you want to access the overlapping matches, take a look at Overlapping matches in R 如果您想访问重叠的匹配项,请查看R中的重叠匹配项
The fn$
prefix in gsubfn package supports string interpolation: gsubfn包中的
fn$
前缀支持字符串插值:
library(gsubfn)
# test data
text <- "ATGGGCTAAATGGGCT"
x <- "GGGC"
fn$gregexpr("(?=$x)", text, perl = TRUE)
See ?fn
, the gsubfn home page and the gsubfn vignette, vignette("gsubfn")
. 请参阅
?fn
, gsubfn主页和gsubfn插图, vignette("gsubfn")
。
ok I solved it in this way: 好吧我用这种方式解决了它:
text="ATGGGCTAAATGGGCT"
x="GGC"
c=paste("(?=",x,")",sep="")
r=gregexpr(c,text,perl=TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.