3
我正在使用空格作爲千位分隔符的文本,例如400或40000或40000000或4000萬000.我需要確定字符串中的數字。一旦識別出來,有很多選項可以重新格式化數字。我是正則表達式的新手。這不起作用:如何使用Python爲字符串中的數字標識空格分隔符?
import re
line = '40) He had 120 hours to increase from 40 000 units to 20 000 000.'
regex = re.compile("(\d+ *\d+)")
re.findall(regex, line)
['40', '120', '40 000', '20 000', '000']
感謝aix。爲了收緊它,我更新了它以限制第一組1-3個數字:re.compile(「(\ d {1,3}(?:\ s * \ d {3})*) 「) – tahoar