你会如何编写一个与每三位数用逗号匹配的正则表达式?

Mau*_*ani 0 python regex

它必须符合以下条件:

  • '42'

  • '1,234'

  • "6368745"

但不是以下内容:

  • '12,34,567'(逗号之间只有两位数)

  • '1234'(缺少逗号)

我在python 3中编写了以下python程序.我在这里做错了什么?它给出了AttributeError

import re
numRegx = re.compile(r"""^
(\d{1,3}(\,))? # optional first three digits and comma (1,)
((d{3})(\,))*  # optional Second three digits and comma (345,)
\d{3}$         # Last three digits (456)
""", re.VERBOSE)
mo = numRegx.search('1,345,456')
print(mo.group())
Run Code Online (Sandbox Code Playgroud)