我试图从字符串中获取数字,忽略所有非数字,如逗号,符号等。我有一个如下所示的正则表达式:
(?:[\d]+)对于"45,00%",结果如下:
Match 1
Full match 0-2 45
Match 2
Full match 3-5 00但我想要4500的完整单人匹配。我该怎么做呢?
发布于 2019-04-24 18:52:03
我们可以先尝试使用re.findall查找所有匹配的百分比字符串,然后对结果列表使用re.sub来去掉小数字符:
input = "Here is one value 45,00% and 12% is another"
matches = re.findall(r'\d+(?:,\d+)?%', input)
matches = [re.sub(r'[,%]', '', i) for i in matches]
print(matches)
['4500', '12']https://stackoverflow.com/questions/55828218
复制相似问题