1

I was trying to extract a substring not followed by "NOT" from a string. For exmaple:

if the string looks like "WKA NOT IN", then the substring should be NA if the string is "WKA abc", then return "WKA".

I tried the str_extract in R with look behind: str_extract(pattern = "WKA (<!NOT)", string)

However, I still got "WKA" from "WKA NOT IN". I can set up rule to return NA by doing something like !grepl("WKA NOT IN", string), but I wonder if there's an easy way to do it? Thanks!

Gillian
  • 175
  • 4

1 Answers1

1

We can create a regex lookaround

library(stringr)
str_extract(str1, "WKA(?! NOT)")
#[1] NA    "WKA"

data

str1 <- c( "WKA NOT IN", "WKA abc")
akrun
  • 874,273
  • 37
  • 540
  • 662