I need to come up with "Regular expression" and a "Substitute" to pad any string that's shorter than 10 characters with zeros. It has to work on regex101.com, PHP flavor. This is all I need.
Example Input:
123
12345
1234567891
Expected output:
0000000123
0000012345
1234567891
I wish it was simple as searching for ([0-9]{1,9}) and replacing it with 000000000$1 but obviously string would exceed length of 10 characters. So I am trying with read ahead syntax but no luck.
As you mentioned in the comments below your question, I provided a .NET method using a catalog to pad a string in regex without using a conditional replacement (see my answer here).
This answer can be adapted to PCRE by using a branch reset group (?|...)
.
Options gJsm
and substitution of ${x}$1
^((?|[1-9](?=.*1\t+(?<x>0+))|[1-9]\d(?=.*2\t+(?<x>0+))|[1-9]\d{2}(?=.*3\t+(?<x>0+))|[1-9]\d{3}(?=.*4\t+(?<x>0+))|[1-9]\d{4}(?=.*5\t+(?<x>0+))|[1-9]\d{5}(?=.*6\t+(?<x>0+))|[1-9]\d{6}(?=.*7\t+(?<x>0+))|[1-9]\d{7}(?=.*8\t+(?<x>0+))))\b
The result:
1
12
123
12345678
123456789
Becomes...
000000001
000000012
000000123
012345678
123456789