Regular expression in Python won't match end of a string

前端 未结 5 580
感动是毒
感动是毒 2021-02-13 12:46

I\'m just learning Python, and I can\'t seem to figure out regular expressions.

r1 = re.compile(\"$.pdf\")
if r1.match(\"spam.pdf\"):
    print \'yes\'
else:
            


        
5条回答
  •  醉话见心
    2021-02-13 13:30

    The regular expression $.pdf says "find the end of the string, then find any character and beyond the any character beyond the end of the string, find a p, a d and an f".

    As written, it cannot sensibly match anything.

    However, pdf$ would match.

    In this specific case, you probably also want to do a search rather than match, as I believe match is inherently anchored at the start of the string.

提交回复
热议问题