2010-05-20 69 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