python - 使用正则表达式提取带有点和逗号的数字

标签 python regex string python-2.7 extract

我已经阅读了很多页面试图解释如何使用Python的regex,但我仍然完全不明白。甚至是regex wikire documentation根本帮不了我。我还是有点困惑:P

我有以下字符串:

string = "|C195|1|Base de Cálculo ST: 2.608,24 - Valor da ST: 163,66|"

我尝试使用以下方法仅提取 2.608,24163,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/

相关文章:

C++,从 ‘char’ 到 ‘const char*’ 的无效转换

python - 添加顶点标签以在 igraph 中绘图

python - 如何用Python实现字典列表的多级排序?

python - 难以使用 Python 2.7 在 Mac 上安装 PyEnchant 模块

python - 指向此页面的 Django Like 按钮不起作用

javascript - 使用 JavaScript RegExp 匹配尾随空格但不匹配空格行

r - 如果它是 "quoted",我如何拆分字符串并忽略分隔符

javascript - 正则表达式匹配 <style> 和 </style> 之间的所有内容,然后用 Javascript 删除

c++ - 如何将 wchar_t 值打印到控制台?

c - 使用 fgets 将字符串从文件流读取到字符数组中。 [C]