The way regex works in Python is so intensely puzzling that it makes me more furious with each passing second. Here\'s my problem:
I understand that this gives a res
The solution is the one you used yourself in the example above: raw strings.
regex = '|'.join(r'\b' + str(state) + r'\b' for state in states)
(Note that I also removed the extra brackets, turning the list comprehension into a generator expression.)