Select an xml code snippet with regex

Asked

Viewed 148 times

0

I need a hand with Regex again! I need to edit an xml file and insert after a certain information a sequence of instructions. But I can’t select all the information I need. I need to select the following information:

    <testerconfirmation title="Tester Confirmation " passedbutton="yes" timeoutresult="failed">REQUIREMENT SPECTED
After T_Checkdisplay (5 seconds), the indication OIL CHANGE REQUEST V3 must be activated according to indications characteristics during 5 seconds.

Press YES to continue.</testerconfirmation>

In the case above I need to select all the information that is inside the tag and contains the information OIL CHANGE REQUEST V3. After finding this information I need to insert some lines of code after the selected information. These are the lines I want to add:

<capltestfunction title="RUN INSPECTION" name="RunInspection">
<caplparam type="string" name="InspName" />
</capltestfunction>
<capltestfunction title="ADD IMAGE TO REPORT" name="AddInspectionImageToReport" />
<capltestfunction title="CHECK ST_F-4" name="CheckStepResultCamera">
<caplparam type="string" name="StepName">ST_F-4</caplparam>
<caplparam type="float" name="ExpVal">1</caplparam>
<caplparam type="float" name="ToleranceUnits" />
<caplparam type="float" name="TolerancePercent" />
</capltestfunction>
<capltestfunction title="CHECK ICON_F-4" name="CheckStepResultCamera">
<caplparam type="string" name="StepName">ICON_F-4</caplparam>
<caplparam type="float" name="ExpVal">1</caplparam>
<caplparam type="float" name="ToleranceUnits" />
<caplparam type="float" name="TolerancePercent" />
</capltestfunction>
<capltestfunction title="CHECK MESSAGE PB058_1" name="CheckStepResultCameraText">
<caplparam type="string" name="StepName">TM_PB058_1</caplparam>
<caplparam type="string" name="ExpVal">OilChange</caplparam>
<caplparam type="int" name="ContainsExpVal" />
<caplparam type="int" name="TolerateSimilarChars">1</caplparam>
</capltestfunction>
<capltestfunction title="CHECK MESSAGE PB058_2" name="CheckStepResultCameraText">
<caplparam type="string" name="StepName">TM_PB058_2</caplparam>
<caplparam type="string" name="ExpVal">Required</caplparam>
<caplparam type="int" name="ContainsExpVal" />
<caplparam type="int" name="TolerateSimilarChars">1</caplparam>
</capltestfunction>

Then the code as follows:

<testerconfirmation title="Tester Confirmation " passedbutton="yes" timeoutresult="failed">REQUIREMENT SPECTED
After T_Checkdisplay (5 seconds), the indication OIL CHANGE REQUEST V3 must be activated according to indications characteristics during 5 seconds.
Press YES to continue.</testerconfirmation>

  <capltestfunction title="RUN INSPECTION" name="RunInspection">
    <caplparam type="string" name="InspName" />
  </capltestfunction>
  <capltestfunction title="ADD IMAGE TO REPORT" name="AddInspectionImageToReport" />
  <capltestfunction title="CHECK ST_F-4" name="CheckStepResultCamera">
    <caplparam type="string" name="StepName">ST_F-4</caplparam>
    <caplparam type="float" name="ExpVal">1</caplparam>
    <caplparam type="float" name="ToleranceUnits" />
    <caplparam type="float" name="TolerancePercent" />
  </capltestfunction>
  <capltestfunction title="CHECK ICON_F-4" name="CheckStepResultCamera">
    <caplparam type="string" name="StepName">ICON_F-4</caplparam>
    <caplparam type="float" name="ExpVal">1</caplparam>
    <caplparam type="float" name="ToleranceUnits" />
    <caplparam type="float" name="TolerancePercent" />
  </capltestfunction>
  <capltestfunction title="CHECK MESSAGE PB058_1" name="CheckStepResultCameraText">
    <caplparam type="string" name="StepName">TM_PB058_1</caplparam>
    <caplparam type="string" name="ExpVal">OilChange</caplparam>
    <caplparam type="int" name="ContainsExpVal" />
    <caplparam type="int" name="TolerateSimilarChars">1</caplparam>
  </capltestfunction>
  <capltestfunction title="CHECK MESSAGE PB058_2" name="CheckStepResultCameraText">
    <caplparam type="string" name="StepName">TM_PB058_2</caplparam>
    <caplparam type="string" name="ExpVal">Required</caplparam>
    <caplparam type="int" name="ContainsExpVal" />
    <caplparam type="int" name="TolerateSimilarChars">1</caplparam>
  </capltestfunction>

I’m using Notepad ++, but if you have any solutions or experience with any other Sw that makes it easy to work with code please leave a comment that will also be welcome! Thank you all!

  • 1

    I don’t understand what you need to select and what you need to replace...

  • Hello @Khaosdoctor, sorry if the explanation was not clear. In fact I need to find the first part of the code (<testerconfirmation up to </testerconfirmation>) and add after this information the second part of the code (which starts in <capltestfunction title="RUN INSPECTION" name="Runinspection">). I hope it helped! Thank you!

  • @Khaosdoctor see if he’s improved?

  • Look, I believe that that will help you, but you’ll have to do some manual work picking up the whole group and replacing it with yourself, plus your new string at the bottom line

  • @Khaosdoctor, I created this regex ( s s s s s s s s s<testerconfirmation.\n.REMOTE START SHUT DOWN V3.\n.\n. *</testerconfirmation>) to select the information I need, but when I replace the selected information ($1+ the rest of the text) it does not replace correctly.

1 answer

0

Try to use this

( *<testerconfirmation(.|\n)*?(?=<\/testerconfirmation>)<\/testerconfirmation>)

Well I’ll explain in parts what each part does.

 *<testerconfirmation

This regex captures all space characters that have the sequence <testerconfirmation after them and the sequence<testerconfirmation

(.|\n)*?

This part captures all characters (including specials) in Lazy form (https://msdn.microsoft.com/en-us/library/3206d374(v=vs.110). aspx)

(?=<\/testerconfirmation>)

A Positive Lookahead to capture up to the < testerconfirmation stretch>

<\/testerconfirmation>

A part to capture </testerconfirmation>, because the Positive Lookahead only determines where the capture sequence should stop.

As for adding that sequence of code, I suggest you use some very simple little application in VS, like using regex to scan the code, take all the occurrences and concatenate the sequence of text you described here to each of them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.