【发布时间】:2013-10-14 14:21:39
【问题描述】:
我正在尝试使用 re.match() 匹配一组复数。我很确定正则表达式应该可以工作,但 Python 仍然没有返回任何内容。
pattern = '\.\d+\.\d+\w\.'
value = str(complex(17, 80))
string = '' + value
print(re.match(pattern, string).group())
输出应该是这样的:
(17+80j)
但实际上是:
AttributeError: 'NoneType' object has no attribute 'group'
【问题讨论】:
-
尝试查看(调试器或打印)你在 re.match(pattern, string) 中有什么我怀疑它是 None
-
match 如果字符串与模式不匹配,则返回
None。
标签: python regex python-3.x