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?
\b
is a zero-width word boundary. Specifically:Example:
.\b
matchesc
inabc
\B
is a zero-width non-word boundary. Specifically:Example:
\B.\B
matchesb
inabc
See regular-expressions.info for more great regex info