title: Pad number with variable length zeroes using REGEX
body: <p>I have a situation where client inputs a number, and I need to validate that number by padding a certain number of zeroes in front of it. The max length of the field that I validate is 9 digits, so if client enters '123', I need to pad it with 6 zeros in front of it and validate. Sounds easy enough, right?</p>
  
  <p>Well, here is the kicker, I have no code-control over any part of the process. The only things available to me are 2 'properties' - a regex pattern and a regex replace that already existing code uses to process input.</p>
  
  <p>So, is it possible to pad an input using a regex pattern with a regex replace? Again, I have no access to the code, so I cannot apply solution such as this <a href="https://stackoverflow.com/questions/11901395/pad-left-with-zeroes">Pad left with zeroes</a> </p>
  
  <p>What I can do is define a search pattern and a replace pattern that already existing code is using.</p>
  
  <p>Here is what I am looking for:</p>
  
  <pre><code>INPUT       OUTPUT
  1           000000001
  12          000000012
  123         000000123
  12345678    012345678
  123456789   123456789
  </code></pre>
  
  <p>Currently what I have for pattern is: </p>
  
  <pre><code>([0-9]{1,9})
  </code></pre>
  
  <p>and for replace:</p>
  
  <pre><code>000000000$1
  </code></pre>
  
  <p>But it doesn't do the job I need, it adds a fixed number of zeroes in front of the input, not a variable number. I suspect the task is impossible, given the restrictions I am working under, but I thought id ask anyway.</p>
  
  <p>PS: the language of the code that reads regex is .NET (C#)</p>
  
  <p><strong>EDIT 1:</strong> Just to reiterate, I cannot change/alter code in any way, I cannot even see it. I assume this is what the existing code does:</p>
  
  <pre><code>  Regex rgx = new Regex(pattern);
    string result = rgx.Replace(input, replacement);
  </code></pre>
  
  <p><code>pattern</code> and <code>replacement</code> are strings being read from a configuration file, and I can change the values of those 2 strings. That's it. I cannot alter code reading it nor can I loop it to regex it twice or anything.</p>
  
  <p><strong>EDIT 2:</strong>
  Not exactly what I'm looking for but (VB.NET code):</p>
  
  <pre><code>Dim result As String = Regex.Replace(input, pattern, Function(match As Match)
                                                           Dim v As String = match.ToString()
                                                           Return String.Format("{0:000000000}", Val(v))
                                                       End Function)
  </code></pre>
  
  <p>Problem is, the <code>Function</code> is code, but if I stick that into property it will be read as a string and will not execute as code but as a replacement string. If I could only figure out how to make that string be treated as code, that would solve my issue.</p>
  
  <p><strong>EDIT 3:</strong> 
  optimized code from EDIT 2(this time in C#):</p>
  
  <pre><code>var result = Regex.Replace(input, pattern, (v) =&gt; string.Format("{0:000000000}", double.Parse(v.Value)));
  </code></pre>
  
  <p>Still cannot figure out how to execute this piece of code <code>(v) =&gt; string.Format("{0:000000000}", double.Parse(v.Value))</code> without changing code on the server (which I have no access to), because it is stored as string. It feels like impossible to do without access to change code on the server.</p>
  
  <p><strong>EDIT 4:</strong>
  Got a look at the server code that executes the regex:</p>
  
  <pre><code>searchVal = Regex.Replace(accountNumber, pattern, replace);
  </code></pre>
  
  <p>The "cannot change code" restriction still applies, so my suggestion from previous edit remains unreachable because it is treated as string and not code. I think this is an impossible task, given the restrictions i am working under.</p>
  
  <p>Thank you all who tried to help</p>