我已经阅读了很多页面试图解释如何使用Python的regex
,但我仍然完全不明白。甚至是regex wiki和 re documentation根本帮不了我。我还是有点困惑:P
我有以下字符串:
string = "|C195|1|Base de Cálculo ST: 2.608,24 - Valor da ST: 163,66|"
我尝试使用以下方法仅提取 2.608,24
和 163,66
:
st_values = re.findall("\d+[,.]\d+", string)
但是,我的 print st_values
的输出是:
['2.608','163,66']
相反,我希望它是
['2.608,24','163,66']
我不想
['195', '1', '2.608,24','163,66']
那么,我如何使用正则表达式参数的字母汤来提取它们?
最佳答案
我建议:
\b\d{1,3}(?:\.\d{3})*,\d+\b
这是一个demo
这是一个 IDEONE code demo :
import re
p = re.compile(r'\b\d{1,3}(?:\.\d{3})*,\d+\b')
test_str = "|C195|1|Base de Cálculo ST: 2.608,24 - Valor da ST: 2.608.234,24 12.608.234,24\n 163,66|\nd2.608.234,24\n2.60d8.23d4,24"
print(re.findall(p, test_str))
关于python - 使用正则表达式提取带有点和逗号的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31455190/