1

I struggle to find a solution for following regex problem. I want to match from the beginning of the string everything with G06 but exclude G06Q. I guess its some kind of lookbehind?

mystring <- c('G06', 'G06Q', 'G11C')

What I want is

[1] "G06"

As a starter I tried following but it need obviously some additional part

grep("^G06", mystring, value=TRUE, perl=TRUE) gives me

[1] "G06" "G06Q"

Similar problem can be found here Link but I struggle to use it for my problem at hand.

CER
  • 854
  • 10
  • 22

1 Answers1

2

We can specify the end ($) of the string

grep("^G06$", mystring, value=TRUE, perl=TRUE)
#[1] "G06"

Or use word boundary (\\b) to mark the end of the word

grep("^G06\\b", mystring, value=TRUE, perl=TRUE)
#[1] "G06"
akrun
  • 874,273
  • 37
  • 540
  • 662