Regex formating?

  • Thread starter Thread starter Glorfindel
  • Start date Start date
G

Glorfindel

If I have a string, say a phone number w/o any formatting in the form
"1234567890"
and want to apply a regular expression to format it, to get "(123) 456-7890"
will any of the .NET classes allow me to do it?
I checked Regex.Replace, but couldnt get it to work, and none of the String
functions do it that I know.
I'm using the string.format and doing it the hard way, but I'd like a
cleaner one.

Thanks,
Eric
 
Glorfindel said:
If I have a string, say a phone number w/o any formatting in the form
"1234567890"
and want to apply a regular expression to format it, to get "(123) 456-7890"
will any of the .NET classes allow me to do it?
I checked Regex.Replace, but couldnt get it to work, and none of the String
functions do it that I know.
I'm using the string.format and doing it the hard way, but I'd like a
cleaner one.

Dim FindPattern As String = "(\d{3})(\d{3})(\d{4})"

Dim ReplacePattern As String = "($1) $2-$3"

Dim InputString As String = "1234567890"

Dim ResultString As String

Dim RX As New Regex(FindPattern)

ResultString = RX.Replace(InputString, FindPattern, ReplacePattern)

Debug.Write(ResultString)
 
Back
Top