Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in R: finding exact number

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

like image 328
user3001378 Avatar asked Jan 26 '26 14:01

user3001378


2 Answers

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".

like image 168
Tim Pietzcker Avatar answered Jan 28 '26 07:01

Tim Pietzcker


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.

like image 37
Casimir et Hippolyte Avatar answered Jan 28 '26 07:01

Casimir et Hippolyte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!