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.

Your email will never ever be published.

Previous:
My trade salary has gone down, apparently June 12, 2005 Work
Next:
Scandinavian Airlines phone booking June 16, 2005 Misc. links
Related by category:
How I run standalone Python in 2025 January 14, 2025 Python
get in JavaScript is the same as property in Python February 13, 2025 Python
How to resolve a git conflict in poetry.lock February 7, 2020 Python
Best practice with retries with requests April 19, 2017 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 July 23, 2005 Python