简体   繁体   English

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

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

Once again, I'm struggling with Regex!再一次,我正在为 Regex 苦苦挣扎! I need to find, in R with the grep function, an expression:我需要在带有grep函数的 R 中找到一个表达式:

  • which includes a dynamic variable (a value that changes in a loop)其中包括一个动态变量(一个在循环中改变的值)
  • and not followed by any character (a dot or a number).并且后面没有任何字符(点或数字)。

Below an example:下面是一个例子:

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))}

In my example, I want to get one.HR0 , twoHR0 in the first loop and so on.在我的示例中,我想在第一个循环中获得one.HR0twoHR0等等。

But my Regex is not working!但是我的正则表达式不起作用! Thanks in advance :-)提前致谢 :-)

Does this do, what you're after?这样做,你在追求什么?


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