I see that there is the \b
which I have never used and I was wondering if someone can give me use cases when it is not possible to do without \b
.
I was wondering if someone can give me use cases when it is not possible to do without \b.
The expression \b
is just a convenient shorthand for what you can already do with other constructs.
For example, if your regular expression engine has lookarounds then \b
is equivalent to the following longer expression:
(?<=\w)(?!\w)|(?<!\w)(?=\w)
Similarly \w
, \d
, etc. are just shorthand for what already can be done using character classes, for example [A-Za-z0-9_]
or [0-9]
. You typically want to use the short version because writing out the full definition each time is cumbersome, harder to read and increases the risk of making an error.
They match on different things - \s
matches on whitespace, \b
on word boundaries.
One good example is the character .
.
In the string hello.hi
:
\s
will not match .
, but \b
will match before and after it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With