Reputation: 67
I'm using Java regex to match "dept." within the string "dept. no. 2" and not getting back any matches using the following regex "\b(dept\.)\b".
It is my understanding that \b would match spaces since they are not word characters, but it doesn't appear to be doing this. When I add \s* to the regex as in the following "\b(dept\.)\s*\b" I get a successful match.
The way that I read my original regex is match dept. (with a literal period) in between word boundaries. Is that not the correct reading of this?
What stupid thing am I not doing/missing here?
Upvotes: 0
Views: 131
Reputation: 20486
\b
matches something along the lines of: (^\w|\w\W|\W\w|\w$)
.
In other words, a word character ([a-zA-Z0-9_]
) next to a non-word character (or at the beginning/end of a String). You are currently trying to use \.\b
to match ". "; the period will match, but it is not followed by a word boundary (since it is two non-word characters).
This is why you can add in the \s*
and it will work, because now the word boundary will be matching the space between " n". I suggest you just use the expression \b(dept\.)
, since \.
is very nearly synonymous to \b
in your scenario (t
a word character, followed by \.
a non-word character).
Upvotes: 3