Tapatalk

How to add leading zeros based on length of current value in an XML file?

How to add leading zeros based on length of current value in an XML file?

2
NewbieNewbie
2

    Nov 03, 2016#1

    I have XML like:

    Code: Select all

    <zip_code>123</zipcode>
    <zip_code>1234</zipcode>
    <zip_code>00123</zipcode>
    
    How can I add leading zeros to every line that has just 1-4 characters between <zip_code> and </zipcode>?

    I can find rows that have 3 chars between using <zip_code>???</zipcode>.

    But how to add zeros to get <zip_code>123</zipcode> modified to <zip_code>00123</zipcode>?

    6,685587
    Grand MasterGrand Master
    6,685587

      Nov 03, 2016#2

      First run a simple non regular expression replace using <zip_code> as search string and <zip_code>0000 as replace string. That changes your example block to:

      Code: Select all

      <zip_code>0000123</zipcode>
      <zip_code>00001234</zipcode>
      <zip_code>000000123</zipcode>
      
      Next run a Perl regular expression replace using backreferences using 0+(\d{5}</zipcode>) as search string and \1 as replace string. That changes your example block to:

      Code: Select all

      <zip_code>00123</zipcode>
      <zip_code>01234</zipcode>
      <zip_code>00123</zipcode>
      
      It is of course also possible to add the leading zeros with the UltraEdit regular expression engine using in total 4 tagged regular expression replaces.

      Replace 1: search for <zip_code>^([0-9]</zipcode>^) and replace with <zip_code>0000^1
      Replace 2: search for <zip_code>^([0-9][0-9]</zipcode>^) and replace with <zip_code>000^1
      Replace 3: search for <zip_code>^([0-9][0-9][0-9]</zipcode>^) and replace with <zip_code>00^1
      Replace 4: search for <zip_code>^([0-9][0-9][0-9][0-9]</zipcode>^) and replace with <zip_code>0^1
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Nov 04, 2016#3

        Thanks that did the job.