问题
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 |
How can I correct that?
回答1:
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)
And then use the referenced groups ($1$2 or \1\2 depending on the flavor).
Edit:
To make sure not to replace dates:
(?<=\d)(?: |\.)(?=\d)(?=[ .\d]*,)
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).
来源:https://stackoverflow.com/questions/16146494/parsing-using-regex-with-2-following-format-csv-file