如何使单词边界 \b 与破折号不匹配 [英] How to make word boundary \b not match on dashes

查看:41
本文介绍了如何使单词边界 \b 与破折号不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将代码简化为我遇到的具体问题.

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- word")

我要了

'-match- match'

但是我想要

'-word- match'

或者对于字符串"word -word-"

我想要

"match -word-"

推荐答案

\b 基本上表示除了 [a-zA-Z0-9_] 也包括空格.用负环顾将 word 包围起来,以确保它前后没有非空格字符:

\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'(?<!\S)word(?!\S)')

这篇关于如何使单词边界 \b 与破折号不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆