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:
re.match()
and re.search()
There is one significant difference: re.match() checks the beginning of string, you are most likely looking for re.search().
Comparison of both methods is clearly shown in the Python documentation chapter called "search() vs. match()"
Also the meaning of characters in regular expressions is different than you are trying to use it (see Regular Expression Syntax for details):
^
matches the beginning:
(Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.
$
matches the end:
Matches the end of the string or just before the newline at the end of the string, and in
MULTILINE
mode also matches before a newline. foo matches both ‘foo
’ and ‘foobar
’, while the regular expressionfoo$
matches only ‘foo
’. More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches ‘foo2
’ normally, but ‘foo1
’ inMULTILINE
mode; searching for a single$
in 'foo\n
' will find two (empty) matches: one just before the newline, and one at the end of the string.
The solution you are looking for may be:
import re
r1 = re.compile("\.pdf$") # regular expression corrected
if r1.search("spam.pdf"): # re.match() replaced with re.search()
print "yes"
else:
print "no"
which checks, if the string ends with ".pdf
". Does the same as kindall's answer with .endswith()
, but if kindall's answer works for you, choose it (it is cleaner as you may not need regular expressions at all).