加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么这个正则表达式只在Python的行开头匹配?

发布时间:2020-12-14 05:38:04 所属栏目:百科 来源:网络整理
导读:参见英文答案 What is the difference between re.search and re.match?????????????????????????????????????8个 在Python中,我可以做到 import rere.match("m","mark") 我得到了预期的结果: _sre.SRE_Match object; span=(0,1),match='m' 但它只有在模式
参见英文答案 > What is the difference between re.search and re.match?????????????????????????????????????8个
在Python中,我可以做到

import re
re.match("m","mark")

我得到了预期的结果:

<_sre.SRE_Match object; span=(0,1),match='m'>

但它只有在模式位于字符串的开头时才有效:

re.match("m","amark")

没有.关于这种模式需要它在字符串的开头 – 没有^或类似的东西.事实上,它在regex101上按预期运作.

Python有一些特殊的行为 – 请问如何禁用它?

解决方法

来自 re.match的文档:

If zero or more characters at the beginning of string match the regular expression pattern,return a corresponding match object.

使用re.search搜索整个字符串.

文档甚至给出了这个问题的章节,概述了两者之间的差异:search() vs. match()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读