User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Find, replace, find in files, replace in files, regular expressions
3 posts Page 1 of 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>?
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 Austria
Thanks that did the job.
3 posts Page 1 of 1