一个非常琐碎的问题-尝试了几次迭代,但仍然无法得到正确的答案。也许"grep“不是正确的命令,或者我的理解完全错误。
我有一根绳子:
"Part 2.2 are Secondary objectives of this study".我正试着在“次要目标”上做一个精确的匹配。我想我可以在这里使用"fixed=TRUE“,但两者都匹配。
> str5 = "Part 2.2 are Secondary objectives of this study"
> line<-grep("Secondary objectives",str5,fixed=TRUE)
> line
[1] 1
> str5 = "Part 2.2 are Secondary objectives of this study"
> line<-grep("Secondary objective",str5,fixed=TRUE)
> line
[1] 1据我所知,"grep“正在做它应该做的事情。它正在搜索字符串“次要目标”,这在技术上是在原来的字符串中。但我的理解是,我可以使用“fixed=TRUE”命令进行精确匹配,但显然我错了。
如果"grep“和"fixed=TRUE”并不是完全匹配的正确命令,那么什么是有效的呢?"str_match“也不起作用。如果我的模式是:“次要目标”,它应该返回“整数(0)”,但是如果我的模式是“次要目标”,它应该返回1。
任何投入都是非常感谢的。非常感谢!-西马克
更新:在下面尝试阿伦的建议
str5 = "Part 2.2 are Secondary objectives of this study"
> grep("(Secondary objectives)(?![[:alpha:]])",str5, perl=TRUE)
[1] 1
> grep("(Secondary objective)(?![[:alpha:]])",str5, perl=TRUE)
integer(0)str5 =“第2.2部分是本研究的次要目标”grep(Pat)(?[:alpha:]),str5,perl=TRUE)整数(0)
However when I did this:
> str5 = "Part 2.2 are Secondary objectives of this study"
> pat <- "Secondary objectives"
> grep("(pat)(?![[:alpha:]])",str5, perl=TRUE)
integer(0)
Thought I can call "pat" inside "grep". Is that incorrect? Thanks!发布于 2013-07-19 10:12:11
我可以想到的一种方法是使用negative lookahead (带有perl=TRUE选项)。也就是说,我们检查模式后面是否没有其他字母表,如果是,则返回1 else不匹配。
grep("(Secondary objective)(?![[:alpha:]])", x, perl=TRUE)
# integer(0)
grep("(Secondary objectives)(?![[:alpha:]])", x, perl=TRUE)
# [1] 1这是可行的,即使你搜索的模式是在最后,因为我们搜索任何不是字母表的东西。那是,
grep("(this stud)(?![[:alpha:]])", x, perl=TRUE)
# integer(0)
grep("(this study)(?![[:alpha:]])", x, perl=TRUE)
# [1] 1https://stackoverflow.com/questions/17743312
复制相似问题