[英]Extracting a number with dot and comma using regex
I've read a lot of pages trying to explain me how to use the regex
for Python but I still don't get it at all. 我读过很多页面,试图向我解释如何将
regex
用于Python,但我还是regex
。 Even the regex wiki and the re documentation couldn't help me at all. 甚至regex Wiki和re文档都根本无法帮助我。 I'm still kind of confused :P
我还是有点困惑:P
I have the following string: 我有以下字符串:
string = "|C195|1|Base de Cálculo ST: 2.608,24 - Valor da ST: 163,66|"
I'm trying to extract only the 2.608,24
and 163,66
using: 我正在尝试使用以下命令仅提取
2.608,24
和163,66
:
st_values = re.findall("\d+[,.]\d+", string)
However, the output of my print st_values
is: 但是,我的
print st_values
的输出是:
['2.608','163,66']
Instead, I expect it to be 相反,我希望它是
['2.608,24','163,66']
I don't want 我不要
['195', '1', '2.608,24','163,66']
So, how can I use the alphabet soup of regex parameters to extract them that way? 那么,如何使用正则表达式参数的字母汤来提取它们呢?
I suggest: 我建议:
\b\d{1,3}(?:\.\d{3})*,\d+\b
And here is an IDEONE code demo : 这是一个IDEONE代码演示 :
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))
Try this (This regex also assumes that strings like 1,23
is matched.)- 试试这个(此正则表达式还假设匹配
1,23
字符串。)
>>> re.findall("\d+(?:\.\d+)?,\d+", string)
['2.608,24', '163,66']
if you want to extract the numbers from the second last column/field, you can do something like: 如果要从倒数第二列/字段中提取数字,则可以执行以下操作:
In: re.findall(r"[0-9,.]+",string.split('|')[-2])
Out: ['2.608,24', '163,66']
Otherwise, if you do it only with regex, and there was similar numbers in other column, you have problem to filter them out. 否则,如果仅使用正则表达式执行此操作,并且其他列中存在相似的数字,则很难将其过滤掉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.