beginner looking for regex to find & replace

  • 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:
    <item name=“Offset” units=“micron” loLimit="-50" hiLimit=“50”> 0</item>

    <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>

  • 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.


  • 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 :


  • @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”


Log in to reply