6

I would like to add a certain number of leading zeroes to a number in a string. For example:

Input: "page 1", Output: "page 001" Input: "page 12", Ouput: "page 012" Input: "page 123", Ouput: "page 123"

What's the best way to do this with Regex.Replace?

At this moment I use this but the results are 001, 0012, 00123.

string sInput = "page 1";
sInput  = Regex.Replace(sInput,@"\d+",@"00$&");
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200

5 Answers5

13

Regex replacement expressions cannot be used for this purpose. However, Regex.Replace has an overload that takes a delegate allowing you to do custom processing for the replacement. In this case, I'm searching for all numeric values and replacing them with the same value padded to three characters lengths.

string input = "Page 1";
string result = Regex.Replace(input, @"\d+", m => m.Value.PadLeft(3, '0'));

On a sidenote, I do not recommend using Hungarian prefixes in C# code. They offer no real advantages and common style guides for .Net advise against using them.

Sven
  • 21,903
  • 4
  • 56
  • 63
  • @ebyrob: maybe show [this document](http://msdn.microsoft.com/en-us/library/ms229045%28v=vs.100%29.aspx) to your boss. It's the official documentation from Microsoft, where we read "Do not use Hungarian notation. Hungarian notation is the practice of including a prefix in identifiers to encode some metadata about the parameter, such as the data type of the identifier". If he doesn't listen to you, maybe he'll listen to creators of the tool which you use? :) – Pawel Krakowiak Apr 16 '14 at 08:57
  • @PawelKrakowiak been there done that... (To his credit we are in a mixed environment, but still.) It was the most I could do to get him a bit educated on systems vs applications Hungarian. –  Apr 16 '14 at 13:05
  • I have this issue but I need to solve this using string values, as it is used as console parameters. Any ideas? – MFedatto Apr 27 '17 at 19:59
4

Use a callback for the replacement, and the String.PadLeft method to pad the digits:

string input = "page 1";
input = Regex.Replace(input, @"\d+", m => m.Value.PadLeft(3, '0'));
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2
var result = Regex.Replace(sInput, @"\d+", m => int.Parse(m.Value).ToString("00#"));
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • Although probably not an issue in this particular case, this code has the danger that it will throw an exception if the input string contains any sequence of digits whose value is larger than `Int32.MaxValue`. – Sven Jul 27 '11 at 10:04
  • @Sven, In context of pages I think it is doubtful assertion :-) – Kirill Polishchuk Jul 27 '11 at 10:10
  • 1
    Which is why I said I didn't think it's an issue in this case. I'm just pointing it out in case someone in the future sees this code and wants to use it for something else. :) – Sven Jul 27 '11 at 10:13
0
string sInput = "page 1 followed by page 12 and finally page 123";

string sOutput = Regex.Replace(sInput, "[0-9]{1,2}", m => int.Parse(m.Value).ToString("000"));
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
0
string sInput = "page 1";
//sInput = Regex.Replace(sInput, @"\d+", @"00$&");
string result = Regex.Replace(sInput, @"\d+", me =>
{
    return int.Parse(me.Value).ToString("000");
});
sloth
  • 99,095
  • 21
  • 171
  • 219
Naga Harish M
  • 2,779
  • 2
  • 31
  • 45