Regex Skip Pattern?

I'm trying to extract the digits and letters following "not" but I don't want to retrieve "not". I know how to do it in Perl or Go, but I'm missing something in R.

Example:

x = "not ab300c_7787 or not ab300c_7038 and not ab300c_7312"
str_extract_all(x1, "not [:word:]+")

returns "not ab300c_7787" "not ab300c_7038" "not ab300c_7312" but I want "ab300c_7787" "ab300c_7038" "ab300c_7312"

Is there an easy way to do this in R?

Thanks.

x = "not ab300c_7787 or not ab300c_7038 and not ab300c_7312"
stringr::str_extract_all(x, "(?<= not )[:word:]+")
#> [[1]]
#> [1] "ab300c_7038" "ab300c_7312"
1 Like

Ah, had to go back another piece. Couldn't figure out a single pass to pick up the first string.

x = "not ab300c_7787 or not ab300c_7038 and not ab300c_7312"
unlist(c(stringr::str_extract(x,"(?<= not )[:word:]+"), stringr::str_extract_all(x, "(?<= not )[:word:]+")))
#> [1] "ab300c_7038" "ab300c_7038" "ab300c_7312"
1 Like

I also like to use str_match() to recover the part of the match in parentheses:

str_match_all(x, "not ([:word:]+)")
1 Like