Find line including string, copy this line and replace numbers in xml fie
-
Hello,
is there any chance in notepad++ to manipulate an xml file in the following manner?the xml file contains multiple lines an different positions conraining the string “>NAME”
The whole line containing this string should be copied directly behind the found line.The same line contains a string "layer=“25"”
After the copying process there are existing always two identical lines with the same content.
In every second line containing "layer=“25"” the “25” should be replaced by “21”.I only managed up to now to bookmark the linemincluding “>NAME” with the Find/Mark Function .
Thanks for your help!
best regards -
Does
>NAME
come beforelayer="25"
in a line? (for example,<component name=">NAME" layer="25">
) or is it reversed? (for example,<shape type="circle" layer="25">NAME CircleOne</shape>
)I’ll assume the second case as that’s more typical with XML files.
Find:
(?-s)^((.*? layer=")(25)(".*?>NAME.*\R))
Replace with:\1\221\4
Example Input:
<?xml version="1.0" encoding="UTF-8"?> ><document> > <shapes> > <shape type="circle" layer="25">NAME CircleOne</shape> > <shape type="square" layer="25">NAME SquareOne</shape> > <shape type="triangle" layer="25">NAME TriangleOne</shape> > <shape type="hexagon" layer="25">NAME HexagonOne</shape> > </shapes> > <layers> > <layer id="1" layer="25">NAME LayerOne</layer> > <layer id="2" layer="25">NAME LayerTwo</layer> > <layer id="3" layer="25">NAME LayerThree</layer> > <layer id="4" layer="25">NAME LayerFour</layer> > </layers> > <components> > <component id="A" layer="25">NAME ComponentA</component> > <component id="B" layer="25">NAME ComponentB</component> > <component id="C" layer="25">NAME ComponentC</component> > <component id="D" layer="25">NAME ComponentD</component> > </components> ></document>
Output:
<?xml version="1.0" encoding="UTF-8"?> <document> <shapes> <shape type="circle" layer="25">NAME CircleOne</shape> <shape type="circle" layer="21">NAME CircleOne</shape> <shape type="square" layer="25">NAME SquareOne</shape> <shape type="square" layer="21">NAME SquareOne</shape> <shape type="triangle" layer="25">NAME TriangleOne</shape> <shape type="triangle" layer="21">NAME TriangleOne</shape> <shape type="hexagon" layer="25">NAME HexagonOne</shape> <shape type="hexagon" layer="21">NAME HexagonOne</shape> </shapes> <layers> <layer id="1" layer="25">NAME LayerOne</layer> <layer id="1" layer="21">NAME LayerOne</layer> <layer id="2" layer="25">NAME LayerTwo</layer> <layer id="2" layer="21">NAME LayerTwo</layer> <layer id="3" layer="25">NAME LayerThree</layer> <layer id="3" layer="21">NAME LayerThree</layer> <layer id="4" layer="25">NAME LayerFour</layer> <layer id="4" layer="21">NAME LayerFour</layer> </layers> <components> <component id="A" layer="25">NAME ComponentA</component> <component id="A" layer="21">NAME ComponentA</component> <component id="B" layer="25">NAME ComponentB</component> <component id="B" layer="21">NAME ComponentB</component> <component id="C" layer="25">NAME ComponentC</component> <component id="C" layer="21">NAME ComponentC</component> <component id="D" layer="25">NAME ComponentD</component> <component id="D" layer="21">NAME ComponentD</component> </components> </document>
If, for some reason,
>NAME
always precedeslayer="25"
, then just swap the elements in the regex like this:(?-s)^((.*?>NAME.*? layer=")(25)(".*\R))
-
@pbarney Thanks for your reply!
I will check your suggestion tomorrow and will reply,
Best regards -
@pbarney
Hi, i tested your example and it worked in Notepad++, but unfortunately not with my xml file.So here a a few example lines of my xml file:
<wire x1=“1.4” y1=“-1.09” x2=“1.4” y2=“0.89” width=“0.1” layer=“21” curve=“-294.794621” cap=“flat”/>
<wire x1=“-0.989” y1=“-0.589” x2=“-0.381” y2=“-0.589” width=“0.0762” layer=“21”/>
<wire x1=“-0.381” y1=“-0.589” x2=“-0.381” y2=“-1.097” width=“0.0762” layer=“21”/>
<pad name=“A” x=“-1.3” y=“0” drill=“0.8” diameter=“1.8”/>
<pad name=“K” x=“1.3” y=“0” drill=“0.8” diameter=“1.8”/>
<text x=“-2.3” y=“2.1” size=“0.8” layer=“25” ratio=“12”>>NAME</text>
<text x=“-0.5” y=“2.1” size=“0.8” layer=“27” ratio=“12”>>VALUE</text>
<text x=“-2” y=“-2.4” size=“0.9” layer=“27” rot=“R90”>>VALUE</text>I hope that you can help me further!
best regards, thanks a lot -
Unfortunately, because you did not use the
</>
button to wrap your text in a “code block”, the forum interprets parts of your XML as HTML, and displays the quotes as smart-quotes, among other formatting changes – so we cannot tell which of the following you actually intended:<wire x1="1.4" y1="-1.09" x2="1.4" y2="0.89" width="0.1" layer="21" curve="-294.794621" cap="flat"/> <wire x1="-0.989" y1="-0.589" x2="-0.381" y2="-0.589" width="0.0762" layer="21"/> <wire x1="-0.381" y1="-0.589" x2="-0.381" y2="-1.097" width="0.0762" layer="21"/> <pad name="A" x="-1.3" y="0" drill="0.8" diameter="1.8"/> <pad name="K" x="1.3" y="0" drill="0.8" diameter="1.8"/> <text x="-2.3" y="2.1" size="0.8" layer="25" ratio="12">>NAME</text> <text x="-0.5" y="2.1" size="0.8" layer="27" ratio="12">>VALUE</text> <text x="-2" y="-2.4" size="0.9" layer="27" rot="R90">>VALUE</text>
vs
<wire x1="1.4" y1="-1.09" x2="1.4" y2="0.89" width="0.1" layer="21" curve="-294.794621" cap="flat"/> <wire x1="-0.989" y1="-0.589" x2="-0.381" y2="-0.589" width="0.0762" layer="21"/> <wire x1="-0.381" y1="-0.589" x2="-0.381" y2="-1.097" width="0.0762" layer="21"/> <pad name="A" x="-1.3" y="0" drill="0.8" diameter="1.8"/> <pad name="K" x="1.3" y="0" drill="0.8" diameter="1.8"/> <text x="-2.3" y="2.1" size="0.8" layer="25" ratio="12">>NAME</text> <text x="-0.5" y="2.1" size="0.8" layer="27" ratio="12">>VALUE</text> <text x="-2" y="-2.4" size="0.9" layer="27" rot="R90">>VALUE</text>
or whether your actual text is something different.
(I could only extract as much as I did because I have moderator powers; even then, I cannot guarantee that the forum didn’t change your text.)
The regex for those two pieces of text is different, so until you clarify, @pbarney may not be able to help you.
—
Useful References
-
oops, the community website alters the pasted xml-file,
her is a screenshot of the original example: -
Sorry for the confusion:
<wire x1="1.4" y1="-1.09" x2="1.4" y2="0.89" width="0.1" layer="21" curve="-294.794621" cap="flat"/> <wire x1="-0.989" y1="-0.589" x2="-0.381" y2="-0.589" width="0.0762" layer="21"/> <wire x1="-0.381" y1="-0.589" x2="-0.381" y2="-1.097" width="0.0762" layer="21"/> <pad name="A" x="-1.3" y="0" drill="0.8" diameter="1.8"/> <pad name="K" x="1.3" y="0" drill="0.8" diameter="1.8"/> <text x="-2.3" y="2.1" size="0.8" layer="25" ratio="12">>NAME</text> <text x="-0.5" y="2.1" size="0.8" layer="27" ratio="12">>VALUE</text> <text x="-2" y="-2.4" size="0.9" layer="25" rot="R90">>VALUE</text>
-
Then I believe changing the FIND WHAT to
(?-s)^((.*? layer=")(25)(".*?>\>NAME.*\R))
should do what you want – it now looks for the closing>
followed by the>
thenNAME
, and will duplicate a line that matches and change the25
in the second to a21
, with the following output:<wire x1="1.4" y1="-1.09" x2="1.4" y2="0.89" width="0.1" layer="21" curve="-294.794621" cap="flat"/> <wire x1="-0.989" y1="-0.589" x2="-0.381" y2="-0.589" width="0.0762" layer="21"/> <wire x1="-0.381" y1="-0.589" x2="-0.381" y2="-1.097" width="0.0762" layer="21"/> <pad name="A" x="-1.3" y="0" drill="0.8" diameter="1.8"/> <pad name="K" x="1.3" y="0" drill="0.8" diameter="1.8"/> <text x="-2.3" y="2.1" size="0.8" layer="25" ratio="12">>NAME</text> <text x="-2.3" y="2.1" size="0.8" layer="21" ratio="12">>NAME</text> <text x="-0.5" y="2.1" size="0.8" layer="27" ratio="12">>VALUE</text> <text x="-2" y="-2.4" size="0.9" layer="25" rot="R90">>VALUE</text>
-
Hello, @ottiditto, @pbarney, @peterjones and All,
A tiny piece of information : The
&
character is not a regex character, so the following syntax is sufficient :-
FIND
(?-s)^((.*? layer=")(25)(".*?>>NAME.*\R))
-
REPLACE
${1}${2}21${4}
Best Regards,
guy038
-