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

前端 未结 5 583
感动是毒
感动是毒 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:47

    Your Question

    $ means "end of string". So, you need a regex like \.pdf$ to match:

    1. A dot (.), escaped because it is a special character in regular expressions.
    2. String "pdf"
    3. End of string.

    Further Reading

    Regular expressions go beyond languages, Python or others, so you should read some tutorials about them firstly. Consider regular-expressions.info. This is not a Python question actually, it is a fundamental regular expression question.

提交回复
热议问题