正则匹配点本身时,为啥匹配不到?

280 天前
 amiwrong123
import re

result = re.match("[\\.]+[\d]+", "...........................................761")
if result is None:
    print(result)
else:
    print("success")

当我执行如上程序时,匹配成功了。

import re

result = re.match("[\\.]+[\d]+", " ...........................................761")
if result is None:
    print(result)
else:
    print("success")

当我执行如上程序时,字符串前面多了个空格,却匹配失败了。

import re

result = re.match("[\\.]+[\d]+", "first chapter...........................................761")
if result is None:
    print(result)
else:
    print("success")

当我执行如上程序时,字符串前面多了个单词,却匹配失败了。

各位大佬,为啥会匹配失败呀?(正则比较菜,大家轻喷)

309 次点击
所在节点    程序员
2 条回复
Inn0Vat10n
280 天前
NoOneNoBody
280 天前
match 是从头开始匹配,从中间开始匹配用 re.search

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/972568

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX