The metacharacter \b
is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. It is common to call an alphanumeric sequence a word. All characters that are not “word characters” are “non-word characters”. Different positions that qualify as word boundaries are
- Before the first character in the string, if the first character is a word character.
- After the last character in the string, if the last character is a word character.
- Between two characters in the string, where one is a word character and the other is not a word character.
The \B
metacharacter is the opposite of \b
, matching against the location of every non-boundary character. Like \b, since it matches locations, it matches no character on its own. It is useful for finding non whole words.
To find match for word, use a regular expression in the form of \bword\b
.
Examples
\bHello\b = Hello \bHello\b ≠ HelloWelcome \bHello\b = Hello!Welcome \bHello = HelloWelcome