Today I learnt about how to use the \B gadget in Python regular expressions. I've previously talked about the usefulness of \b but there's a big benefit to using \B sometimes too.

What \b does is that it is a word-boundary for alphanumerics. It allows you to find "peter" in "peter bengtsson" but not "peter" in "nickname: peterbe". In other words, all the letters have to be grouped prefixed or suffixed by a wordboundry such as newline, start-of-line, end-of-line or a non alpha character like (.

What \b does for finding alphanumerics, \B does for finding non-alphanumerics. Example:


>>> import re
>>> re.compile(r'\bX\b').findall('X + Y') 
['X'] # it can find 'X'
>>> re.compile(r'\b\+\b').findall('X + Y')
[] # same technique can't find '+'
>>> re.compile(r'\B\+\B').findall('X + Y')
['+'] # better to use \B when finding '+'
>>> re.compile(r'\BX\B').findall('X + Y')
[] # and use \B only for non-alphanumerics

The lesson is: \b is a really useful tool but it's limited to finding alphanumerics (numbers and A-Z). \B is what you have to use for finding non-alphanumerics.

Comments

Your email will never ever be published.

Previous:
London bus 26 from Hackney July 21, 2005
Next:
Release package file size July 29, 2005 IssueTrackerProduct
Related by category:
A Python dict that can report which keys you did not use June 12, 2025 Python
In Python, you have to specify the type and not rely on inference October 10, 2025 Python
Native connection pooling in Django 5 with PostgreSQL June 25, 2025 Python
Combining Django signals with in-memory LRU cache August 9, 2025 Python
Related by keyword:
UPPER vs. ILIKE April 19, 2010 Web development
CSS selector simplifier regular expression in JavaScript December 20, 2017 Web development, JavaScript
Advanced live-search with AngularJS February 4, 2014 JavaScript
\b in Python regular expressions June 14, 2005 Python