【问题标题】:Match set of string representations of Python complex numbers匹配 Python 复数的字符串表示形式
【发布时间】: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


【解决方案1】:

试试这个:

pattern = r'.\d+.\d+\w.'

【讨论】:

  • 所以最终我对模式的看法是错误的 :))) 谢谢 Alfe!
猜你喜欢
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 2011-11-28
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
相关资源
最近更新 更多