R中的正则表达式:找到确切的数字

时间:2014-04-04 14:02:48

标签: regex r string-matching

这是在R

grep("AB22", c("AB22" ,"AB22","AB22" ,"AB22+3" ,"AB226AEM+1","AB22AEM+2") , value=T) 

给出了所有这些: " AB22"," AB22"," AB22" " AB22 + 3" " AB226AEM + 1" " AB22AEM + 2"

但是,我只想要" AB22"," AB22"," AB22" " AB22 + 3" ,AB22AEM + 2"即所有条目包含AB22而不是AB226或2265 ......等。

由于

3 个答案:

答案 0 :(得分:2)

这是word boundary anchors和/或negative lookahead assertion的工作:

grep("\\bAB22(?!\\d)", c("AB22" ,"AB22","AB22" ,"AB22+3" ,"AB226AEM+1","AB22AEM+2") , value=T, perl=TRUE);

(?!\d)表示“断言在当前位置之后无法匹配数字”。

答案 1 :(得分:1)

您可以使用:

grep("AB22[^0-9]|AB22$", c("AB22" ,"AB22","AB22" ,"AB22+3" ,"AB226AEM+1","AB22AEM+2") , value=T)

或更短:

grep("AB22([^0-9]|$)", c("AB22" ,"AB22","AB22" ,"AB22+3" ,"AB226AEM+1","AB22AEM+2") , value=T)

如果需要,您可以在开头添加起始锚^

答案 2 :(得分:0)

How to make grep only match if the entire line matches?

我认为这篇文章可能有用。

在搜索字符串的开头(^)和结尾($)处使用锚点会将grep限制为与搜索字符串完全匹配的返回结果。

grep("^AB22$", "AB22" ,"AB22","AB22".....