Regex in R: finding exact number

耗尽温柔 提交于 2021-02-05 06:03:22

问题


This is in R

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

gives all of them: "AB22","AB22", "AB22" ,"AB22+3" ,"AB226AEM+1" ,"AB22AEM+2"

but, I want only "AB22","AB22","AB22" ,"AB22+3" ,AB22AEM+2" i.e. all the entries containing AB22 and not AB226 ot 2265...etc.

Thanks


回答1:


That's a job for word boundary anchors and/or a negative lookahead assertion:

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

(?!\d) means "Assert that it's impossible to match a digit after the current position".




回答2:


You can use this:

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

or shorter:

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

if needed you can add the start anchor ^ at the begining.




回答3:


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

I think this post might be of some use.

Using anchors at the start(^) and end($) of your search string will limit grep to returning results that match your search string exactly.

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


来源:https://stackoverflow.com/questions/22865000/regex-in-r-finding-exact-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!