David Moore
David Moore

Reputation: 968

Negative lookbehind regular expression in R

I have the following vector:

vector_3 <- c("oh me oh my", "hello", "oleo", "how ostensible")

I want to know which element contains an e that isn't preceded by an h.

I tried

grep("(?<!h).*e", vector_3, perl = T)

but this didn't work. The code should return 3, since the third element is the only element that contains an e that isn't preceded by an h.

What am I doing wrong?

Upvotes: 1

Views: 181

Answers (1)

Roland
Roland

Reputation: 132706

grep("^[^h]*e", vector_3, perl = TRUE)
#[1] 3

Your first problem is that .* can include an "h". This can be fixed by a negated character class. If you use that, you don't need the lookbehind.

You also need a start of string anchor. Otherwise every "e" would be a match.

Upvotes: 3

Related Questions