Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Python re.match不匹配以“quot;结尾的字符串;。“数字”;_Regex_Python 2.7 - Fatal编程技术网

Regex Python re.match不匹配以“quot;结尾的字符串;。“数字”;

Regex Python re.match不匹配以“quot;结尾的字符串;。“数字”;,regex,python-2.7,Regex,Python 2.7,作为更大代码的一部分,我试图检查字符串(文件名)是否以结尾 然而,re.match(re.compile and match)只是不匹配字符串末尾的模式 代码: import re f = ".1.txt.2" print re.match('\.\d$',f) 输出: >>> print re.match('\.\d$',f) None 任何帮助都将不胜感激 使用搜索而不是匹配 从 re.match()仅在字符串开头检查匹配项, 而re.search()检查字符串中的任

作为更大代码的一部分,我试图检查字符串(文件名)是否以结尾 然而,re.match(re.compile and match)只是不匹配字符串末尾的模式

代码:

import re
f = ".1.txt.2"
print re.match('\.\d$',f)
输出:

>>> print re.match('\.\d$',f)
None

任何帮助都将不胜感激

使用
搜索
而不是
匹配


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

你可以试试这个

import re
word_list = ["c1234", "c12" ,"c"]
for word in word_list:
    m = re.search(r'.*\d+',word)
    if m is not None:
        print(m.group(),"-match")
    else:
        print(word[-1], "- nomatch")

可能重复的哇-是的。令人惊讶的我认为
re.match('^..*(\.\d)$',f)。第(1)组
也会起作用。