>>> import re
>>> b = re.findall(r'^\d{,3}(,\d{3})*','12,344,567')
>>> b
[',567']


我希望得到['12,344,567'],但实际输出是[',567']。哪里出了问题?提前致谢。

最佳答案

您需要使用?:,否则正则表达式仅捕获括号内的内容:

^\d{,3}(?:,\d{3})*

例:
import re

b = re.findall(r'^\d{,3}(?:,\d{3})*','12,344,567')
print(b)
# ['12,344,567']

关于python - 正则表达式匹配数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57104845/

10-12 21:13