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!
Every Monday, you'll get a Python deep dive that unpacks a topic with analogies, diagrams, and code examples so you can write clearer, faster, and more idiomatic code.