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
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 ;
.
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.