繁体   English   中英

带有变量的正则表达式,后面没有 R 中的任何内容

[英]Regex with a variable and not followed by anything in R

再一次,我正在为 Regex 苦苦挣扎! 我需要在带有grep函数的 R 中找到一个表达式:

  • 其中包括一个动态变量(一个在循环中改变的值)
  • 并且后面没有任何字符(点或数字)。

下面是一个例子:

for (j in 1:2){
     aaa = c(0,-0.5)
     JOP = c("one.HR0","twoHR0", "oneHR0.5", "twoHR0.5") 
     print(grep(paste0("HR", abs(aaa[j]),"?!."), JOP))}

在我的示例中,我想在第一个循环中获得one.HR0twoHR0等等。

但是我的正则表达式不起作用! 提前致谢 :-)

这样做,你在追求什么?


for (j in 1:2){
  aaa = c(0,-0.5)
  JOP = c("one.HR0","twoHR0", "oneHR0.5", "twoHR0.5") 
  print(grep(paste0("HR", abs(aaa[j]),"$"), JOP, value=T)) #value=T returns actual values and not only the matching indexes in JOP
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM