Reputation: 51
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
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.
2 33 456 789012
becomes 00000002 000000033 0000000456 0000000789012
.In more detail.
\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.\b0+(\d{7})\b
and replace with \1
.Notes
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.Upvotes: 1
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
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