xmlsearchreplacevim

Vim Search, Replace And Match Length Of Numbers By Appending Zeroes


I have a pattern in XML that I want to search and then replace by appending zeroes such that all the replaced numbers will have same length.

Pattern to search is as shown below:

<title>99
<title>1
<title>1232
<title>414212

After search and replace, the output should be as follows:

<title>0000099
<title>0000001
<title>0001232
<title>0414212

I can search and replace using below option:

:%s/title>/title>0/g

But, as expected, is only going to append only single 0 in front of the numbers.

Using Vim only, is it possible to execute a search and replace query, such that it not only appends zero but also as many so that all the occurrence have dynamic zeroes appended to match the length?

The size of the XML file I have is 3GB, so using Vim so far seems to be an efficient option.


Solution

  • The following command will pad all the numbers with leading zeros:

    :%s/title>\zs\d\+/\=printf('%08s', submatch(0))
    

    We match "title>" and then use the \zs atom to start the match, everything after this will be captured. That's one or more digits \d\+. We then use \= to substitute with an expression. Our expression is the printf() function with a format of %08s, i.e. a string padded with zeros to eight characters. Our second argument is submatch(0) which contains the digits we just captured.

    Related documentation can be found in :help /\zs, :help :s\= and :help printf().

    Note that some programs interpret numbers with leading zeros as octal and may stumble on something like 099.