简体   繁体   English

查找/替换正则表达式以重新排列 Notepad++ 中的文本

[英]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:这应该是 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=\"(.*?)\"查找: DATA1=\"(.*?)\"|DATA2=\"(.*?)\"|DATA3=\"(.*?)\"|DATA4=\"(.*?)\"|DATA5=\"(.*?)\"

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

and

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

REPLACE: $+{d1} $+{d2} $+{d3} $+{d4} $+{d5}替换: $+{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)如果有人可以帮助或指导我找到正确的答案,我会很高兴(并且很抱歉任何误解,因为英语不是我的第一语言)

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).此正则表达式通过使用可选的前瞻来定位DATAx (其中x是数字)并将"内的值捕获到捕获组中,然后匹配整行(以替换它)。

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] .此替换仅引用捕获组并在它们之间添加制表符,同时按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请参阅此处使用的正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM