python - 正则表达式字符串不匹配

标签 python regex

我在用 Python 匹配字符串中的数字时遇到问题。虽然它应该明显匹配,但它甚至不匹配 [0-9] [\d] 或仅匹配 0。我的疏忽在哪里?

import re

file_without_extension = "/test/folder/something/file_0"

if re.match("[\d]+$", file_without_extension):
   print "file matched!"

最佳答案

阅读文档:http://docs.python.org/2/library/re.html#re.match

If zero or more characters at the beginning of string

您想使用re.search(或re.findall)

关于python - 正则表达式字符串不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16569067/

相关文章:

regex - 如何在 Notepad++ 中使用正则表达式替换字符串,同时保留其一部分

python - 无法让我的脚本有条件地等待

python - Py-StackExchange 引发 valueError

正则表达式匹配除某些单词外的所有小写字符

java - 检查字符串中除特殊符号外的所有字符是否均为大写

java - 在使用 Java 中的正则表达式从日志中提取表达式时需要帮助

regex - 如何消除文件名中的点,文件扩展名除外

python 回合问题

python - 我需要一个类来创建位于磁盘上的字典文件

python - python中多个列表的乘积总和