在 R 中,我如何 grep 字符串中有 $ 的内容。在下面的示例中,我需要 grep 字符串“NB6106$MU-MU.rma”
x<-c("NB6106$MU-MU.rma", "NB610634$MU-MU.rma")
x[grep(pattern="*6106$*.rma", x = x)] #does not work
最佳答案
你可以使用
x<-c("NB6106$MU-MU.rma", "NB610634$MU-MU.rma")
x[grep(pattern="6106\\$.*\\.rma", x = x)]
参见 R demo
详情
6106\\$
-6106$
子串.*
- 任何 0+ 个字符\\.rma
-.rma
子串
如果您打算确保不使用 grep 11116106$...rma
,您可以使用
"(^|\\D)6106\\$.*\\.rma$"
其中 (^|\\D)
匹配字符串开头 (^
) 或 (|
) 非数字字符 (\D
) 和结尾处的 $
确保字符串的结尾紧跟在 .rma
之后。
关于r - 在字符串中用 $ grep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52701093/