Python 3正则表达式正确,但语法错误?

问题描述 投票:0回答:1

我是regex的新手,必须在语法上缺少一些愚蠢的东西

我的代码:

import re

numberString = '42 1,234 342,111,234 23,23,34 1234'#regex to match first 3 values and not last 2

regtext = re.compile(r'^\d{1,3}(,\d{3})*$')

 #^\d{1,3}   Find a 1 to 3 digit number to start

 #(,\d{3})*$ Find 0 or more cases of a comma and three digits at the end

matchingNumbers = regtext.findall(numberString)

我尝试过不同版本的正则表达式,并使用了几种在线正则表达式工具,说匹配是正确的,但它为我返回了一个空字符串。

python regex findall
1个回答
0
投票

如果要进行多个匹配,则不应使用^$

regtext = re.compile(r'\d{1,3}(,\d{3})*')
© www.soinside.com 2019 - 2024. All rights reserved.