python:re.Match(r&quot“ 1” [1]&quot返回空的
为什么Python的Regex模块在以下代码中不识别“ [1]”?
In [43]: re.match (r"1", "[1]")
In [44]:
而这有效:
In [46]: re.match (r"1", "123")
Out[46]: <re.Match object; span=(0, 1), match='1'>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 docs.pys.python.org :
由于您的“字符串”以
开头[
,因此未捕获。正如文档所建议的那样,使用
re.search(r“ 1”,“ [1]”)
确实与之匹配,因为它可以匹配字符串中的任何位置这里的“字符串” 以开始,并按预期匹配
From docs.python.org:
Since your 'string' starts with an
[
, it's not captured.As suggested by the docs, using
re.search(r"1", "[1]")
does match it since it will match anywhere in the stringHere the 'string' starts with the pattern and is being matched as expected