Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was \b introduced while \s match string borders in regular expressions?

Tags:

regex

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.

like image 201
Alexander Suraphel Avatar asked Dec 27 '22 14:12

Alexander Suraphel


2 Answers

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.

like image 56
Mark Byers Avatar answered Jun 07 '23 11:06

Mark Byers


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.

like image 45
Oded Avatar answered Jun 07 '23 11:06

Oded