I am reading a book on regular expression and I came across this example for \b:
The cat scattered his food all over the room.
Using regex – \bcat\b will match the word cat but not the cat in scattered.
For \B the author uses the following example:
Please enter the nine-digit id as it
appears on your color – coded pass-key.
Using regex \B-\B matches - between the word color - coded. Using \b-\b on the other hand matches the - in nine-digit and pass-key.
How come in the first example we use \b to separate cat and in the second use \B to separate -? Using \b in the second example does the opposite of what it did earlier.
Please explain the difference to me.
EDIT: Also, can anyone please explain with a new example?
The confusion stems from your thinking
\bmatches spaces (probably because “b” suggests “blank”).\bmatches the empty string at the beginning or end of a word.\Bmatches the empty string not at the beginning or end of a word. The key here is that “-” is not a part of a word. So<left>-<right>matches\b-\bbecause there are word boundaries on either side of the-. On the other hand for<left> - <right>(note the spaces), there are not word boundaries on either side of the dash. The word boundaries are one space further left and right.On the other hand, when searching for
\bcat\bword boundaries behave more intuitively, and it matches ” cat ” as expected.