The special characters \b and \B go hand in hand in regular expressions:
\b matches at word boundaries; and\B matches inside words.For these two characters, the default βword charactersβ are alphanumeric characters and the underscore.
By combining \b and \B at the beginning or end of a pattern, you get to match standalone words, prefixes, suffixes, and infixes!
The table below shows some examples of sentences that all contain the substring "legal" along the rows.
The columns show whether different patterns that use the special characters \b and \B would match against those sentences.
r"legal" |
r"\blegal\b" |
r"\blegal\B" |
r"\Blegal\b" |
r"\Blegal\B" |
|
|---|---|---|---|---|---|
"Criticism is legal." |
β | β | β | β | β |
"He's legally blind." |
β | β | β | β | β |
"Theft is illegal." |
β | β | β | β | β |
"He obtained that illegally." |
β | β | β | β | β |
This was the tip 97 I sent to the Python drops ππ§ newsletter, so if you'd like to get a daily drop of Python knowledge, make sure to sign-up now!
Get ready for 12 intense days of problem-solving. The βAlgorithm Mastery Bootcampβ starts December 1st and it will feature 24 programming challenges, live analysis sessions, a supportive community of like-minded problem-solvers, and more! Join now and become the Python expert others can rely on.