Reputation: 2961
I'm working on some regular expressions using an online Regex tester (http://www.regexr.com) to help figure out the syntax. However when I try to evaluate the pattern in R, I get a different result from what I was expecting.
The code is:
grepl("Brent[^a-zA-Z]", c("Brent", "Brentwood", "Brent 01"))
and I'm expecting: TRUE, FALSE, TRUE
which is what I get with the online tester. But R returns FALSE, FALSE, TRUE
.
What am I missing here?
Upvotes: 1
Views: 62
Reputation: 51430
The first string cannot match because [^a-zA-Z]
has to consume exactly one character.
You can replace it with \b
(word boundary): \bBrent\b
.
And, if you want to allow strings like Brent01
(without the space before the 01
), you can use lookaheads: \bBrent(?![a-zA-Z])
(but I don't know if this feature is available in R).
Upvotes: 2