我有一个数据框,其中某些行包含单词“ Comments(0)”。我想获取所有出现“注释”且括号内为任意数字的事件。
我正在使用下面的代码,它给了我integer(0)作为输出。
text <- c("Because I could not Comments(2) stop for Death - Comments(1)",
"He kindly stopped for me -",
"The Carriage held but just Ourselves - Comments(5)",
"and Immortality")
grep(pattern = "Comments([:digit:])", text)
您可以使用:
grep(pattern = "Comments\\(\\d\\)", text)
#output
[1] 1 3
要么
grep(pattern = "Comments\\([[:digit:]]\\)", text)
在(
和)
需要进行转义,因为它们是正则表达式的特殊字符
\\d
- 数字
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句