tba
tba

Reputation: 81

Changing order of csv-file entries with regex replacement in Notepad++

I am trying to change the order of the entries of an *.csv-file with Notepad++ built-in find/replace function. This is how the file looks like now:

ABC;DEF;Here comes some long text with ,.- in it;true;false;
QWE;RTY;Here comes some long text with ,.- in it;true;false;

And this is how it should look like after find/replace:

DEF;Here comes some long text with ,.- in it;ABC;true;false;;
RTY;Here comes some long text with ,.- in it;QWE;true;false;;

So column #1 should be at the position of #3, column number #2 and #3 should shift one to the left.

What I tried so far:

I tried to get the first three columns with an regular expression in the find field, put some brackets around them and reorder them with the $ sign in the replace field. But my regex matches for nearly the whole line, not only the first three columns- what am I doing wrong? Here is my regex:

([A-Z]{3})\;([A-Z]{3})\;(.*[^\;])\;

The first two columns and the following ; are select properly, the problem must be in the third round bracket. But I have no clue what the problem is. The third expression should match to everything except ; and is ended by an ;.

The content of the replacement field should be $2;$3;$1;, I guess that's right.

Upvotes: 2

Views: 814

Answers (1)

JonM
JonM

Reputation: 1374

The main problem is that you're escaping the semi-colons unnecessarily. Use this expression ^(?s)([A-Z]{3};)([A-Z]{3};)([^\n\r;]*;) and replace it with this expression $2$3$1

Have included line delimiters \r or \n too in case of a line with fewer columns. Also you should use start of string anchor ^ to be safe if you have more columns.

Upvotes: 3

Related Questions