How to make word boundary \b not match on dashes

后端 未结 3 465
滥情空心
滥情空心 2021-01-05 04:05

I simplified my code to the specific problem I am having.

import re
pattern = re.compile(r\'\\bword\\b\')
result = pattern.sub(lambda x: \"match\", \"-word-          


        
3条回答
  •  礼貌的吻别
    2021-01-05 04:38

    \b basically denotes a word boundary on characters other than [a-zA-Z0-9_] which includes spaces as well. Surround word with negative lookarounds to ensure there is no non-space character after and before it:

    re.compile(r'(?

提交回复
热议问题