Notepad++: Add leading zero(s) to lines but up to 9 digits

Solution 1:

Adapted from Leading Zeros on Notepad++ community forum:

  • Find what: ^(\d{9})|(\d{8})|(\d{7})|(\d{6})|(\d{5})|(\d{4})|(\d{3})|(\d{2})|(\d)$

  • Replace with: (?{1}\1)(?{2}0\2)(?{3}00\3)(?{4}000\4)(?{5}0000\5)(?{6}00000\6)(?{7}000000\7)(?{8}0000000\8)(?{9}00000000\9)

Solution 2:

This can be done in two steps:

First, prefix all lines with zeroes so all lines contain at least 9 digits:

Search: ^
Replace: 000000000
[x] Regular expression
[ ] . matches newline

^ means: The beginning of a line

Second, use this regex to cut off excess zeroes from the beginning of all lines:

Search: ^0*(\d{9})$
Replace: \1

The search regex means: Find lines that start with any number of zeroes 0*, followed by exactly 9 digits \d{9} before the end of the line $. The 9 digits are marked as a group ( ) which can be referenced in the replace box as \1. The entire match (i.e. the whole line) will be replaced with the contents of this group.