Python re.match 不匹配以“.number”结尾的字符串

Ama*_*mar 3 regex python-2.7

作为更大代码的一部分,我试图检查字符串(文件名)是否以“.number”结尾 。但是,re.match(重新编译和匹配)不会匹配字符串末尾的模式。

代码:

import re
f = ".1.txt.2"
print re.match('\.\d$',f)
Run Code Online (Sandbox Code Playgroud)

输出:

>>> print re.match('\.\d$',f)
None
Run Code Online (Sandbox Code Playgroud)

任何帮助都感激不尽 !

Col*_*der 5

使用search而不是match


来自https://docs.python.org/2/library/re.html#search-vs-match

re.match() 仅在字符串开头检查匹配,而 re.search() 检查字符串中任何位置的匹配。