作为更大代码的一部分,我正在尝试检查 string(filename) 是否以 ".number" 结尾 但是,re.match(重新编译和匹配)不会匹配字符串末尾的模式。
代码:
import re
f = ".1.txt.2"
print re.match('\.\d$',f)
输出:
>>> print re.match('\.\d$',f)
None
任何帮助将不胜感激!
最佳答案
使用搜索
代替匹配
来自 https://docs.python.org/2/library/re.html#search-vs-match
re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string.
关于regex - Python re.match 不匹配以 ".number"结尾的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41083894/