Deformatting phone numbers using regular expressions

Public Class Deformatters

‘Note: This was based on the Nokia keypad layout

‘Some phones use the #1 key for space, Q, & Z

Public Shared Function Extract(ByVal InputString As String, ByVal RegExpres As String) As String

Return Regex.Replace(InputString, RegExpres, “”, RegexOptions.ECMAScript)

End Function

Public Shared Function DeformatPhonenumber(ByVal PhoneNumber As String) As String

Return Extract(PhoneNumber, “[^a-zA-Z0-9]”)

End Function

Public Shared Function DecodePhonenumber(ByVal PhoneNumber As String) As String

PhoneNumber = DeformatPhonenumber(PhoneNumber)

PhoneNumber = Regex.Replace(PhoneNumber, “[abcABC]”, “2”, RegexOptions.ECMAScript)

PhoneNumber = Regex.Replace(PhoneNumber, “[defDEF]”, “3”, RegexOptions.ECMAScript)

PhoneNumber = Regex.Replace(PhoneNumber, “[ghiGHI]”, “4”, RegexOptions.ECMAScript)

PhoneNumber = Regex.Replace(PhoneNumber, “[jklJKL]”, “5”, RegexOptions.ECMAScript)

PhoneNumber = Regex.Replace(PhoneNumber, “[mnoMNO]”, “6”, RegexOptions.ECMAScript)

PhoneNumber = Regex.Replace(PhoneNumber, “[pqrsPQRS]”, “7”, RegexOptions.ECMAScript)

PhoneNumber = Regex.Replace(PhoneNumber, “[tuvTUV]”, “8”, RegexOptions.ECMAScript)

PhoneNumber = Regex.Replace(PhoneNumber, “[wxyzWXYZ]”, “9”, RegexOptions.ECMAScript)

Return PhoneNumber

End Function

End Class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.