\B
is the negated version of \b
. \B
matches at every position where \b
does not. Effectively, \B
matches at any position between two word characters as well as at any position between two non-word characters.
\b
allows you to perform a “whole words only” search using a regular expression in the form of \bword\b
. A “word character” is a character that can be used to form words. All characters that are not “word characters” are “non-word characters”. It is more exact to say \b
matches before and after an alphanumeric sequence
Above is from here.
In your example of \bdog\B
, it will match “doggie” because you asked it to match words and partial words.
\bdog\b
will fail on “doggie” because “dog” is the whole word whereas “dog” in “doggie” is a partial word.
It’s conditional depending on what it’s touching.
Moreover, dog\B
will match “dog” in the phrase “I have a doggie”
dog\b
will match “dog” in the phrase “I have adog gie”
dog\B
will not match “dog” in the phrase “I have adog gie”
\bdog\b
will not match “dog” in the phrase “I have adog gie”
but \Bdog\b
will match “dog” in the phrase “I have adog gie”
This SO answer provides a detailed explanation.
solved Regex in java \bword\B which metacharacter win? [closed]