2024-03-29 15:56:43 发布
网友
在下面的代码中,我希望程序打印“Match”,因为"\D+\d"匹配字符串的"x4"部分。但它不打印任何东西。有什么问题?你知道吗
"\D+\d"
"x4"
import re pattern = r"\D+\d" if re.match(pattern, "1x4"): print("Match");
谢谢
您认为re.match可以匹配字符串中的任何位置的假设是错误的。你知道吗
re.match
https://docs.python.org/2/library/re.html#re.RegexObject.match
If zero or more characters at the beginning of string match this regular expression, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.
MatchObject
None
改用re.search()。你知道吗
re.search()
您认为
re.match
可以匹配字符串中的任何位置的假设是错误的。你知道吗https://docs.python.org/2/library/re.html#re.RegexObject.match
改用
re.search()
。你知道吗相关问题 更多 >
编程相关推荐