2010-05-20 65 views

回答

3

您不需要正则表达式来执行此操作。您可以使用string.PadLeft

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

如果你需要使用正则表达式(可能是因为您正在执行一些更复杂的更换,其中,这只是一小部分),那么你可以组合使用MatchEvaluator上述技术:

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

结果:

 
foo 012423 bar 
+0

谢谢。我担心这是不可能的。 – norbertB 2010-05-20 13:39:19