Parsing using Regex with 2 following format (CSV File)

淺唱寂寞╮ 提交于 2019-12-25 04:08:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!