regex - Python re.match 不匹配以 ".number"结尾的字符串

标签 regex python-2.7

作为更大代码的一部分,我正在尝试检查 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/

相关文章:

python - python中计数器的总和列表

python - 不进入python 2.7.2中的for循环

C++ 无法让 regex_match 工作

Python 正则表达式搜索不适用于包含冒号的字符串(:)

python - 字符串格式化 - Python

python-2.7 - python 烤宽面条 ImportError 无法导入 BatchNormLayer

python - Python 如何自省(introspection)激活了哪些语言功能?

regex - Grep 和正则表达式 : inconsistent behaviour

c# - 正则表达式 XAML 问题

php - PHP字符串控制台参数数组