使用Regex进行以下2种格式的解析(CSV文件) [英] Parsing using Regex with 2 following format (CSV File)
问题描述
我有一个平面文件(CSV),其值用"|"
分隔,我想转换所有具有特定格式" 1 234 567,89 "或" 1.123"的数字.456,89 "更改为" 1234567,89 "
I have a flat file (CSV) with values separated by "|"
, and i would like to convert all numbers with specific format "1 234 567,89" or "1.123.456,89" into "1234567,89"
为此,我创建了此正则表达式规则:
In order to do that i created this regex rule:
(\|\ *)([0-9]{0,3})(\.|\ )?([0-9]{3})?(\.|\ )?([0-9]{3})?(,)?([0-9]{0,3})(-| )?(\|)
这很好用,除非连续两个数字,例如:
this works fine, except when 2 numbers are continus, for example:
| 9 450,000 |**9 809 100,000** | 1 890,000 |UN |
我该如何纠正?
推荐答案
如果您使用的正则表达式允许两种环视效果,我认为这可能就足够了:
If the regex flavor you're using allow both lookarounds, I think this could be enough:
(?<=\d)(?: |\.)(?=\d)
然后用空字符串替换匹配项就足够了.
但是,如果您不能使用它们,则仍然可以使用
And then replacing the matches by an empty string may be enough.
However, if you can't use them, you can still use
(\d)(?: |\.)(\d)
然后使用引用的组($1$2
或\1\2
取决于口味).
And then use the referenced groups ($1$2
or \1\2
depending on the flavor).
修改:
为确保不替换日期,请执行以下操作:
To make sure not to replace dates:
(?<=\d)(?: |\.)(?=\d)(?=[ .\d]*,)
在使用csv文件时,您的数据由|
分隔,因此可以正常工作(因为我不确定您的全部数据,所以我不确定).
As you're using a csv file, your data are separated by |
so that can work (as I don't have your entire data, I'm not sure).
这篇关于使用Regex进行以下2种格式的解析(CSV文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!