3

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

user3001378
  • 306
  • 1
  • 2
  • 10

3 Answers3

2

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

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

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.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

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".....
Community
  • 1
  • 1