问题描述
s = '!sopa !sop !sopaa !sopii'
如何在使用词边界时忽略!
re.sub(r'\b\!sop\b', 'sopa', s)
输出:'!sopa !sop !sopaa !sopii'
您似乎想要这样的东西.
>>>s = '!sopa !sop !sopaa !sopii'>>>re.sub(r'\B!sop\b', 'sopa', s)'!sopa sopa !sopaa !sopii'您的正则表达式将失败,因为在 !
符号之前实际上没有 \b
退出.也就是说,从上面你试图匹配 !
符号,前提是它前面有一个非单词字符.\b
匹配单词 char 和非单词字符,反之亦然.\B
匹配两个单词和两个非单词字符.这里 \B
实际上存在于空格和 !
之间,因为它们都是非单词字符.
s = '!sopa !sop !sopaa !sopii'
how to ignore !
when using word boundary
re.sub(r'\b\!sop\b', 'sopa', s)
output : '!sopa !sop !sopaa !sopii'
Seems like you want something like this.
>>> s = '!sopa !sop !sopaa !sopii'
>>> re.sub(r'\B!sop\b', 'sopa', s)
'!sopa sopa !sopaa !sopii'
Your regex will fail because there isn't a \b
actually exits before !
symbol. That is, from the above you're trying to match the !
symbol only if it's preceded by a non-word character. \b
matches between a word char and a non-word character, vice versa. \B
matches between two word and two non-word chars. Here \B
is actaully exists between a space and !
, since both are non-word characters.
这篇关于使用单词边界时如何忽略特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!