views:

22

answers:

1

I've got a string of digits that is either 4 or 5 digits long and it needs to be padded with "0" till it's 6 digits long. Is this possible? I'm using .Net framework.

+3  A: 

You don't need a regular expression to perform this operation. You can use string.PadLeft:

s = s.PadLeft(6, '0');

If you need to use regular expression (perhaps because you are performing some more complex replacement of which this is just a small part) then you can use a MatchEvaluator in combination with the above technique:

string s = "foo <12423> bar";
s = Regex.Replace(s, @"<(\d+)>", match => match.Groups[1].Value.PadLeft(6, '0'));

Result:

foo 012423 bar
Mark Byers
Thanks. I was afraid it wasn't possible.
norbertB