Notepad++ find value with regex and replace to value that find with another regex

huangapple go评论65阅读模式
英文:

Notepad++ find value with regex and replace to value that find with another regex

问题

You can replace the value between <id1> and <id1> with the value from <id2> using Notepad++ and regular expressions. Here's a possible regex pattern to find and replace:

Find what:

<id1>(.*?)<\/id1>.*?<id2>(.*?)<\/id2>

Replace with:

<id1><\/id1>

This regex pattern captures the values between <id1> and <id2> and replaces the content between <id1> with the content between <id2>.

英文:

i have a string:

...some values...&lt;id1&gt;10052023&lt;/id1&gt;...some others...&lt;id2&gt;306689461795&lt;/id2&gt;...

i need to replace value between id1 with value from id2, is it possible to do with npp?

i know about groups but i don't know how to use it properly in this case.

my value can be found and stored in group 1 with regex like this:

^.*&lt;id1&gt;(.*)&lt;/id1&gt;.*$

am i right?

答案1

得分: 0

Here is the translated content:

使用 xmlstarlet(适用于所有平台):

$ cat file
&lt;root&gt;
...一些值...&lt;id1&gt;10052023&lt;/id1&gt;...其他一些值...&lt;id2&gt;306689461795&lt;/id2&gt;...
&lt;/root&gt;

&lt;p&gt;

$ xmlstarlet edit -d &#39;//id1/text()&#39; -u &#39;//id1&#39; -x &#39;//id2/text()&#39; file
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;root&gt;
...一些值...&lt;id1&gt;306689461795&lt;/id1&gt;...其他一些值...&lt;id2&gt;306689461795&lt;/id2&gt;...
&lt;/root&gt;

请注意,我已将XML代码部分保持不变。

英文:

With xmlstarlet (available on all platforms):

$ cat file
&lt;root&gt;
...some values...&lt;id1&gt;10052023&lt;/id1&gt;...some others...&lt;id2&gt;306689461795&lt;/id2&gt;...
&lt;/root&gt;

<p>

$ xmlstarlet edit -d &#39;//id1/text()&#39; -u &#39;//id1&#39; -x &#39;//id2/text()&#39; file
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;root&gt;
...some values...&lt;id1&gt;306689461795&lt;/id1&gt;...some others...&lt;id2&gt;306689461795&lt;/id2&gt;...
&lt;/root&gt;

huangapple
  • 本文由 发表于 2023年5月6日 22:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定