Community
    • Login

    beginner looking for regex to find & replace

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 1.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • krat choufK
      krat chouf
      last edited by

      I need to replace stuff in the offset tag (unit, loLimit and hiLimit) only when the parent tag is Caliper.
      The new tag would be like this:
      new:
      <item name=“Offset” units=“micron” loLimit=“-50” hiLimit=“50”> 0</item>

      old:
      <group name=“Caliper”>
      <item name=“Target” units=“gsm” loLimit=“1” hiLimit=“800”> 220</item>
      <item name=“Standard” units=“gsm” loLimit=“1” hiLimit=“800”> 220</item>
      <item name=“ProfOffSpecLimit” units=“gsm” loLimit=“0” hiLimit=“50”> 10</item>
      <item name=“AvgOffSpecLimit” units=“gsm” loLimit=“0” hiLimit=“50”> 10</item>
      <item name=“Slope” loLimit=“0.8” hiLimit=“1.2”> 1</item>
      <item name=“Offset” units=“gsm” loLimit=“-25” hiLimit=“-25”> 0</item>
      <item name=“CustConvUnits” units=“gsm” loLimit=“-5.0” hiLimit=“5.0”> 1</item>
      </group>

      1 Reply Last reply Reply Quote 1
      • Terry RT
        Terry R
        last edited by

        I’ve done a bit of testing and I think the following regex will fill your requirements. It just looks for the word ‘caliper’ following the ‘group name’ and then captures all the characters up to the word ‘offset’. This is capture group 1. Then it catches the remaining characters up to the ‘/item>’ string. These characters aren’t captured, instead the replace field puts the group 1 characters back followed by the replacement text you provided.

        Note that at the start is the ‘(?is)’ expression. This allows for upper and lower case characters (the i, so it could be caliper or Caliper or even caLIpeR) and to capture over several lines (the s) without having to identify EOL characters.

        Use the Replace operation with ‘regular expression’ and ‘wrap around’ selected. You can either use the ‘replace all’ button, or use the ‘replace’ button so you can see each change as it takes place. If you run it a second time it will edit the same groups, although the edits will just replace what you’ve already changed to the same text, so it won’t harm the groups.

        Find what: (?is)(<group name\=“caliper”>.+?“offset”).+?</item>
        Replace with: \1 units=“micron” loLimit="-50" hiLimit=“50”> 0</item>

        It is best to copy the red text above and paste into the fields as I noticed the quotation marks aren’t the regular ones I’d use.
        Hope this solves your issue.

        Terry

        1 Reply Last reply Reply Quote 1
        • krat choufK
          krat chouf
          last edited by

          Hi Terry,

          Thank you for your help. It worked with the correct double quote.

          I used this: \1 units=“micron” loLimit=“-50” hiLimit=“50”>
          Because I don’t want to touch the 0</item> as this number is different for each offset tag. But by replacing with this regex I lost the end of the line.

          How can I keep the 0</item> or ex: -13.5</item> as it is?

          The original file : https://ufile.io/o6m8k

          Regards,
          krat

          Claudia FrankC 1 Reply Last reply Reply Quote 0
          • Claudia FrankC
            Claudia Frank @krat chouf
            last edited by Claudia Frank

            @krat-chouf

            one way would be to define the remaining part as part of a regex lookahead,
            which basically means something like: this only matches if the following text follows.
            So in your case and what has been already discussed you could modify it like

            find: (?is)(<group name\=“caliper”>.+?“offset”).+?(?=>.*?</item>)
            replace: \1 units=“micron” loLimit="-50" hiLimit=“50”

            Cheers
            Claudia

            1 Reply Last reply Reply Quote 3
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors