Matching commas after the last semicolon on a row

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

I have a CSV file that has data separated by semicolons. In the last column in the file, I have a list of numbers separated by a comma. I wan't to replace these commas with a semicolon so they become their own columns.

How do I match every comma after the last semicolon on each row? I can't just replace all commas with semicolons, because some other columns in the file contain commas too.

I am trying to replace them in Notepad++.

189;1;data here, can contain commas;311,232,161,132,371

That should become

189;1;data here, can contain commas;311;232;161;132;371

回答1:

You could use something like this:

(?:;(?!.*;)|(?!^)\G)[^,]*\K,

Replace with:

;

;(?!.*;) matches the last ;. It's a ;, which is not followed by another ;.

(?!^)\G is used to match at the end of a previous match.

(?:;(?!.*;)|(?!^)\G) will mean either match the last ;, or at the start of the previous match.

[^,]* will match non commas, and lastly, \K resets the match to allow you match only the commas.

regex101 demo

Note: Not all versions of Notepad++ support \G and \K (I don't remember exactly which one was implemented first though, probably \G).


The above is more... what you described. A workaround could be this:

,(?!.*;)

Match a , that is not followed by a ; ahead. And replace with ;.



回答2:

This regex matches a comma that does not have a semicolon between it and end of line:

,(?=[^;]*$)

If you want to delete such commas, replace matches with a blank.



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