gpt4 book ai didi

python - 为什么 match 返回 None 而 findall 不返回?

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:33 25 4
gpt4 key购买 nike

我有这个字符串:

s = "mage('Images/mpins/pin5_Jul1.png', new"

这是我的模式:

patt_img = r'\w+.png'

为什么

re.findall(patt_img,s)

返回

['pin5_Jul1.png']

但是 match 返回 None

m = re.match(patt_img,s)
>>> type(m)
<type 'NoneType'>`

最佳答案

因为 match 只从字符串的开头开始匹配。

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

If you want to locate a match anywhere in string, use search() instead.

关于python - 为什么 match 返回 None 而 findall 不返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16913721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com