\b in Python regular expressions

14 June 2005   3 comments   Python

Mind That Age!

This blog post is 13 years old! Most likely, its content is outdated. Especially if it's technical.

Boy did that shut me up! The \b special character i python regular expressions is so useful. I've used it before but have forgotten about it. The following code:

def createStandaloneWordRegex(word):
   """ return a regular expression that can find 'peter'
   only if it's written alone (next to space, start of 
   string, end of string, comma, etc) but not if inside 
   another word like peterbe """
   return re.compile(r"""
     (
     ^ %s
     (?=\W | $)
     |
     (?<=\W)
     %s
     (?=\W | $)
     )
     """% (re.escape(word), re.escape(word)),
           re.I|re.L|re.M|re.X)

can with the \b gadget be simplified to this:

def createStandaloneWordRegex(word):
   """ return a regular expression that can find 'peter'
   only if it's written alone (next to space, start of 
   string, end of string, comma, etc) but not if inside 
   another word like peterbe """
   return re.compile(r'\b%s\b' % word, re.I)

Quite a lot simpler isn't it? The simplified passes all the few unit tests I had.

Comments

Matt Schinckel
Excellent! I was wondering how to do this for a script (http://schinckel.blogsome.com/2005/06/27/ecto-auto-abbracronym/) that automatically adds abbr and acronym tags to text in ecto, a blogging client for MacOS X.
YuppY
First variant could be shorter:

re.compile(r'((?<=\W)|^)%s(?=\W|$)' % re.escape(word), re.I)

re.escape is necessary.
Peter Bengtsson
shorter but certainly less readable.
Thank you for posting a comment

Your email will never ever be published


Related posts

Previous:
My trade salary has gone down, apparently 12 June 2005
Next:
Scandinavian Airlines phone booking 16 June 2005
Related by Keyword:
CSS selector simplifier regular expression in JavaScript 20 December 2017
Advanced live-search with AngularJS 04 February 2014
UPPER vs. ILIKE 19 April 2010
Quick PostgreSQL optimization story 11 March 2006
Are you a web developer? Then VisiBone is for you 22 January 2006
Related by Text:
HTML Tree on Hacker News 18 May 2014
YouTube - Nigella's XXXmas 19 December 2008
How and why to use django-mongokit (aka. Django to MongoDB) 08 March 2010
How to track Google Analytics pageviews on non-web requests (with Python) 03 May 2016
hashin 0.7.0 and multiple packages 30 August 2016