2

I want to pick up the pattern mn followed by two numbers.

My text is :

apple 20127041 needs the following changes from ab34 to mn35-f01 priority - mn43 mn56.

The expected output is mn43 and mn56. It should not pick up mn35-

I am not able to negate the -.

dd <- c("apple 20127041 needs the following changes from ab34 to mn35-f01 priority - mn43 mn56 ")
str_extract_all(dd,'\\bmn[0-9]{2}\\b')

It is picking up - value as well.

thelatemail
  • 85,757
  • 12
  • 122
  • 177
R Ban
  • 95
  • 9
  • Relevant discussion about what exactly a word boundary is in regex - https://stackoverflow.com/questions/1324676/what-is-a-word-boundary-in-regex – thelatemail May 13 '20 at 09:10

2 Answers2

2

Add a non consuming negative look ahead of (?!-):

stringr::str_extract_all(dd,'\\bmn[0-9]{2}\\b(?!-)')
#[1] "mn43" "mn56"
GKi
  • 27,870
  • 2
  • 18
  • 35
0
str_extract_all(dd,'mn[0-9]{2}[^-]')
[[1]]
[1] "mn43 " "mn56 "

Exclude the - from those characters allowed to appear behind the numbers. To get rid of the white space, use trimwsand unlist:

trimws(unlist(str_extract_all(dd,'mn[0-9]{2}[^-]')))
[1] "mn43" "mn56"
Chris Ruehlemann
  • 15,379
  • 3
  • 11
  • 27