user9221392
user9221392

Reputation: 13

Find/Replace regex to rearrange text in Notepad++

I have certain data that I want to rearrange (it's all on the same line) I have tried multiple approaches but I can't get it to work.

Here is an example of the text:

DATA1="8DE" DATA2="322" DATA3="20" DATA4="19.99" DATA5="0.01"
DATA1="FE4" DATA2="222" DATA4="400" DATA3="400" DATA5="0.00"
DATA1="CE3" DATA2="444" DATA4="60" DATA5="0.00" DATA3="60"
DATA1="MME" DATA3="20" DATA4="20" DATA5="0.00"
DATA2="667" DATA4="30" DATA3="30" DATA5="0.00" DATA1="MH4"

This should be the output:

8DE     322     20      19.99   0.01
FE4     222     400     400     0.00
CE3     444     60      60      0.00
MME             20      20      0.00
MH4     667     30      30      0.00

I have tried the following but to no avail:

FIND: DATA1=\"(.*?)\"|DATA2=\"(.*?)\"|DATA3=\"(.*?)\"|DATA4=\"(.*?)\"|DATA5=\"(.*?)\"

REPLACE: \1 \2 \3 \4 \5

and

FIND: DATA1=\"(?<d1>.*?)\"|DATA2=\"(?<d2>.*?)\"|DATA3=\"(?<d3>.*?)\"|DATA4=\"(?<d4>.*?)\"|DATA5=\"(?<d5>.*?)\"

REPLACE: $+{d1} $+{d2} $+{d3} $+{d4} $+{d5}

I would be happy if someone can help or direct me to the right answer (and sorry for any misunderstanding as english is not my first languaje)

Upvotes: 1

Views: 161

Answers (1)

ctwheels
ctwheels

Reputation: 22817

The regex

^(?=.*\bDATA1="([^"]+)"\h*)?(?=.*\bDATA2="([^"]+)"\h*)?(?=.*\bDATA3="([^"]+)"\h*)?(?=.*\bDATA4="([^"]+)"\h*)?(?=.*\bDATA5="([^"]+)"\h*)?.*

This regex works by using optional lookaheads to locate DATAx (where x is the number) and capturing the value inside the " into a capture group, then matching the whole line (in order to replace it).

The replacement

$1\t\t$2\t\t$3\t\t$4\t\t$5

This replacement just references the capture groups and adds tab characters between them while reordering them in the order of DATA [1,2,3,4,5].

The result

8DE     322     20      19.99       0.01
FE4     222     400     400     0.00
CE3     444     60      60      0.00
MME             20      20      0.00
MH4     667     30      30      0.00

See it working

See the regex in use here

Upvotes: 2

Related Questions