A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mathspp.com/blog/til/matching-prefixes-and-suffixes-with-regex below:

TIL #128 – Matching prefixes and suffixes with regex

Matching prefixes and suffixes with regular expressions

The special characters \b and \B go hand in hand in regular expressions:

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!

Become a better Python 🐍 developer, drop by drop 💧

Get a daily drop of Python knowledge. A short, effective tip to start writing better Python code: more idiomatic, more effective, more efficient, with fewer bugs. Subscribe here.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4