Simon Griffee

\b in Python regular expressions ➶

30 June 2011

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

Send