Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在用 Python 匹配字符串中的数字时遇到问题。虽然它应该明确匹配,但它甚至不匹配[0-9] [\d]或只是0单独匹配。我的疏忽在哪里?
[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
如果字符串开头有零个或多个字符
你想使用re.search(或re.findall)
re.search
re.findall
re.match被“锚定”到字符串的开头。使用re.search.
re.match
使用 re.search 而不是 re.match。