Re模块中的Python正则表达式是否支持单词边界(\b)?在尝试更多地了解正则表达式的同时,一个教程建议您可以使用\b匹配一个词的边界。但是,Python解释器中的以下代码段不像预期的那样工作:>>> x = 'one two three'
>>> y = re.search("\btwo\b", x)如果匹配的话,它应该是一个Match对象,但是它是None.是\bPython中不支持的表达式,还是我使用它的错误?
3 回答

陪伴而非守候
TA贡献1757条经验 获得超8个赞
word = 'two're.compile(r'\b%s\b' % word, re.I)
>>> word = 'two'>>> k = re.compile(r'\b%s\b' % word, re.I)>>> x = 'one two three'>>> y = k.search( x)>>> y<_sre.SRE_Match object at 0x100418850>
>>> x = 'one two three'>>> y = re.search(r"\btwo\b", x)>>> y<_sre.SRE_Match object at 0x100418a58>>>>

凤凰求蛊
TA贡献1825条经验 获得超4个赞
re.search(r"\btwo\b", x)
"\b"
"\x08"
"\\b"
r"\b"

青春有我
TA贡献1784条经验 获得超8个赞
re.search("\btwo\b", x)
\b
print("foo\bbar")fobar
"\btwo\b"
two
x = 'one two three'
re.search
compile
\b
"\\btwo\\b"
r"\btwo\b"
).
添加回答
举报
0/150
提交
取消