Why my regex with r'string' matches but not 'string' using Python?

前端 未结 4 1137
滥情空心
滥情空心 2020-12-10 15:25

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

4条回答
  •  隐瞒了意图╮
    2020-12-10 15:54

    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.)

提交回复
热议问题