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
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With