user3095679
user3095679

Reputation: 51

Notepad++ find and replace number, digit format

I have single and two digit number, and I want to make it 4 digits.

So "1" become "0001", "22" become "0022"

How do I do that?

Upvotes: 2

Views: 13819

Answers (3)

AdrianHHH
AdrianHHH

Reputation: 14076

Generalising the question. Suppose we want to convert a collection of numbers so that all have, say, 7 digits by adding leading zeroes as necessary. This can be done in two steps.

  1. Add 7 leading zeroes to each number, so eg 2 33 456 789012 becomes 00000002 000000033 0000000456 0000000789012.
  2. Convert each number to have the required number of digits by removing some leading zeroes to leave the wanted 7 digits.

In more detail.

  1. Search for \b(\d{1,6})\b which finds numbers with between 1 and 6 digits inclusive. Replace them with 0000000\1. There is no need to search for 7-digit numbers as they are already the correct length.
  2. Search for \b0+(\d{7})\b and replace with \1.

Notes

  • Input numbers that have more than 7 digits will not be found by step 1.
  • Input numbers that have leading zeros with less than 7 significant digits, e.g. 001234 will have the 7 zeroes added by step 1 whereas 00000000000001234 is longer than 7 digits and so will not be changed by step 1.
  • Input numbers with leading zeroes and more than 7 significant digits will not be changed.

Upvotes: 1

Antonio N
Antonio N

Reputation: 46

I used \1 instead of $1

First Replacement:

search: (\d\d)
replace: 00\1

Second Replacement:

search: (\d)
replace: 000\1

Upvotes: 0

Toto
Toto

Reputation: 91518

You have to do two replacements:

search: \b(\d\d)\b
replace: 00$1

and:

search: \b(\d)\b
replace: 000$1

Upvotes: 3

Related Questions