Format string using regular expressions

  • Thread starter Thread starter Alexander Vasilevsky
  • Start date Start date
A

Alexander Vasilevsky

There is a phone number in a string such as "3223223" to format it as a
"322-32-23", using regular expressions. Is it possible?

http://www.alvas.net - Audio tools for C# and VB.Net developers
 
There is a phone number in a string such as "3223223" to format it as a
"322-32-23", using regular expressions. Is it possible?

Regular expressions are used to parse strings, not to format them. Can
you clarify why you think you need them here?

In the specific case that you give, what's wrong with this:

var result = string.Format("{0}-{1}-{2}", input.Substring(0, 3),
input.Substring(3, 2), input.Substring(5, 2));
 
Back
Top