Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does match returns None while findall doesn't?

I have this string:

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

This is my pattern:

patt_img = r'\w+.png'

Why does

re.findall(patt_img,s)

return

['pin5_Jul1.png']

but match returns None?

m = re.match(patt_img,s)
>>> type(m)
<type 'NoneType'>`
like image 902
LarsVegas Avatar asked May 10 '26 18:05

LarsVegas


1 Answers

Because match matches only starting from the beginning of the string.

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.

like image 128
kirelagin Avatar answered May 13 '26 09:05

kirelagin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!