问题
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