Python Regex Word Boundaries not working as expected

python, regex

Solution

You need to use a raw-string for your Regex pattern (which does not process escape sequences):

>>> import re
>>> a = 'Builders Club The Ohio State'
>>> re.sub(r'\bThe\b', '', a, flags=re.IGNORECASE)
'Builders Club  Ohio State'
>>>

Otherwise, `\b` will be interpreted as a backspace character:

>>> print('x\by')
y
>>> print(r'x\by')
x\by
>>>

Problem

Why isn't the word boundary working? reading this site, I know a word boundary works like this: There are three different positions that qualify as word boundaries: - Before the first character in the string, if the first character is a word character. - After the last character in the string, if the last character is a word character. - Between two characters in the string, where one is a word character and the other is not a word character. The `a` string below appears to fit at least one of the positions listed above. ``` a = 'Builders Club The Ohio State' re.sub('\bThe\b', '', a, flags=re.IGNORECASE) ``` output. There is no change in the 'The'. ``` 'Builders Club The Ohio State' ``` Why isn't the word boundary working? When I put spaces before and after ' The ' pattern, the regex appears to work. ``` a = 'Builders Club The Ohio State' re.sub(' The ', ' ', a, flags=re.IGNORECASE) ``` output: ``` 'Builders Club Ohio State' ```

Original source

Related problems