473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

phone number regex

I have phone numbers like this in a data table

123-435-1234
1231231234
432.234.2321

they all have different formatting, what I want to do is get them all
formatted like this

(123) 123-1234

Regex.Replace(Convert.ToString(drRow("Phone")), "(\d{3})(\d{3})(\d{4})",
"($1) $2-$3")

seems to work to do that only when there are no special characters in it...
how would i remove the special characters then do the formatting? thanks!
Nov 21 '05 #1
4 4506
June 22, 2005

Dim number as string = PHONE NUMBER SOURCE

number.replace("-","")
number.replace(" ","")
number.replace(".","")

Now all the numbers will be NNNNNNNNNN...

Now use the substring to get the numbers.... I think the signature is
correct in my example...

number = "(" & number.substring(0,3) & ") " & number.substring("3,3) & "-"
& number.substring(6,4)

If I got the substring sign. right, it should produce (NNN) NNN-NNNN

Hope this helps and have a great day!

--
Joseph Bittman
Microsoft Certified Application Developer

Web Site: http://71.35.110.42
Dynamic IP -- Check here for future changes

"Brian Henry" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I have phone numbers like this in a data table

123-435-1234
1231231234
432.234.2321

they all have different formatting, what I want to do is get them all
formatted like this

(123) 123-1234

Regex.Replace(Convert.ToString(drRow("Phone")), "(\d{3})(\d{3})(\d{4})",
"($1) $2-$3")

seems to work to do that only when there are no special characters in
it... how would i remove the special characters then do the formatting?
thanks!

Nov 21 '05 #2

"Joseph Bittman MCAD" <Ry*********@msn.com> wrote in message
news:ef*************@tk2msftngp13.phx.gbl...
June 22, 2005

Dim number as string = PHONE NUMBER SOURCE

number.replace("-","")
number.replace(" ","")
number.replace(".","")

Now all the numbers will be NNNNNNNNNN...

Now use the substring to get the numbers.... I think the signature is
correct in my example...

number = "(" & number.substring(0,3) & ") " & number.substring("3,3) &
"-" & number.substring(6,4)

If I got the substring sign. right, it should produce (NNN) NNN-NNNN

Hope this helps and have a great day!

--
Joseph Bittman
Microsoft Certified Application Developer

Web Site: http://71.35.110.42
Dynamic IP -- Check here for future changes

"Brian Henry" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I have phone numbers like this in a data table

123-435-1234
1231231234
432.234.2321

they all have different formatting, what I want to do is get them all
formatted like this

(123) 123-1234

Regex.Replace(Convert.ToString(drRow("Phone")), "(\d{3})(\d{3})(\d{4})",
"($1) $2-$3")

seems to work to do that only when there are no special characters in
it... how would i remove the special characters then do the formatting?
thanks!


pattern = "^.*(\d{3}).*(\d{3}).*(\d{4}).*$"

This would allow anything, for example:

abc123def456ghi7890
(123) 456-7890

HTH :)

Mythran

Nov 21 '05 #3
We do something like this: (Note that *we here* do not translate letters to
numbers, as in 1-800-CALL-NOW):

' This is one of my classes that does dozens
' of different kinds of string manipulation
Public Class Transform

Private _cleanPhone As New Regex("\D")

' Other declarations...

' Note that since strings are also immutable, I pass
' the input to this method as ByRef instead of ByVal,]
' for greater speed.
Public Sub CleanPhoneNumber(ByRef value As String)
value = _cleanPhone.Replace(value, "")
End Sub

' Other Methods...

End Class

So in the client, if I do this:

Dim tx As New Transform()
Dim phone As String
' load my data table (dt)

For Each dr As DataRow In dt.Rows
phone = dr("phone").ToString()
phone = tx.CleanPhoneNumber(phone)

' now I have 1234567890
' and then I have line like Joseph's code snip:
dr("phone") = _
"(" & phone.substring(0,3) & ") " & _
phone.substring("3,3) & "-" & _
phone.substring(6,4)

' now I have (123) 456-7890

Next

da.Update(dt) ' give it to a SqlDataAdapter
Note that this format only works with NANP (3 digits)-(7 digits) phone
numbers, as are found in the US, Canada and a few other countries. Here's a
good reference for other international phone numbers: http://www.wtng.info

If you have to deal with international phone numbers, you will have to build
a table of all countries you're interested in, that contains a country code
or name, International Prefix, Trunk Prefix and maybe some other data from
the web site if it concerns you.

I cannot share details because of intellectual property issues, but I spent
4 months last year developing a phone number parsing system compatible with
auto-dialers that can reliably dialy any phone number on the planet *from*
anywhere on the planet.

The above website was my guide to all the telephone mysteries. :)

While I'm here, I will point out that if you use Regex.Replace() directly
without creating an instance of Regex, and I bet you're doing this in a
loop... That's bad.

The static methods of the Regex class incur the overhead of creating and
destroying a Regex instance. This means that if you do something like this:

For Each dr As DataRow In dt.Rows
Regex.Replace(...)
Next

What you're doing is the same as this:

For Each dr As DataRow In dt.Rows
Dim rx As New Regex(...)
rx.Replace(...)
rx = Nothing
Next

So for something like an immutable Regex instance, you should declare an
instance of it and *then* run your loop:

Dim rx As New Regex(...)

For Each dr As DataRow In dt.Rows
rx.Replace(...)
Next

The difference between the first loop and the last loop is that the first
one will create and destroy one instance of Regex per each record in your
DataTable. The last loop, directly above, use only a single instance of
Regex. The difference between the two depends on how many records you're
processing. In our case, the difference was astronomical. When we clean
phone numbers, it could be close to a million at a time. Just imagine what
the previous loop would have done to the Garbage Collector! :)
--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
Nov 21 '05 #4
thanks for the code, but i was looking for a pure regex one like Mythran
provided.
"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:ep**************@TK2MSFTNGP15.phx.gbl...
We do something like this: (Note that *we here* do not translate letters
to numbers, as in 1-800-CALL-NOW):

' This is one of my classes that does dozens
' of different kinds of string manipulation
Public Class Transform

Private _cleanPhone As New Regex("\D")

' Other declarations...

' Note that since strings are also immutable, I pass
' the input to this method as ByRef instead of ByVal,]
' for greater speed.
Public Sub CleanPhoneNumber(ByRef value As String)
value = _cleanPhone.Replace(value, "")
End Sub

' Other Methods...

End Class

So in the client, if I do this:

Dim tx As New Transform()
Dim phone As String
' load my data table (dt)

For Each dr As DataRow In dt.Rows
phone = dr("phone").ToString()
phone = tx.CleanPhoneNumber(phone)

' now I have 1234567890
' and then I have line like Joseph's code snip:
dr("phone") = _
"(" & phone.substring(0,3) & ") " & _
phone.substring("3,3) & "-" & _
phone.substring(6,4)

' now I have (123) 456-7890

Next

da.Update(dt) ' give it to a SqlDataAdapter
Note that this format only works with NANP (3 digits)-(7 digits) phone
numbers, as are found in the US, Canada and a few other countries. Here's
a good reference for other international phone numbers:
http://www.wtng.info

If you have to deal with international phone numbers, you will have to
build a table of all countries you're interested in, that contains a
country code or name, International Prefix, Trunk Prefix and maybe some
other data from the web site if it concerns you.

I cannot share details because of intellectual property issues, but I
spent 4 months last year developing a phone number parsing system
compatible with auto-dialers that can reliably dialy any phone number on
the planet *from* anywhere on the planet.

The above website was my guide to all the telephone mysteries. :)

While I'm here, I will point out that if you use Regex.Replace() directly
without creating an instance of Regex, and I bet you're doing this in a
loop... That's bad.

The static methods of the Regex class incur the overhead of creating and
destroying a Regex instance. This means that if you do something like
this:

For Each dr As DataRow In dt.Rows
Regex.Replace(...)
Next

What you're doing is the same as this:

For Each dr As DataRow In dt.Rows
Dim rx As New Regex(...)
rx.Replace(...)
rx = Nothing
Next

So for something like an immutable Regex instance, you should declare an
instance of it and *then* run your loop:

Dim rx As New Regex(...)

For Each dr As DataRow In dt.Rows
rx.Replace(...)
Next

The difference between the first loop and the last loop is that the first
one will create and destroy one instance of Regex per each record in your
DataTable. The last loop, directly above, use only a single instance of
Regex. The difference between the two depends on how many records you're
processing. In our case, the difference was astronomical. When we clean
phone numbers, it could be close to a million at a time. Just imagine
what the previous loop would have done to the Garbage Collector! :)
--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: joemono | last post by:
Hello everyone! First, I appologize if this posting isn't proper "netiquette" for this group. I've been working with perl for almost 2 years now. However, my regular expression knowledge is...
5
by: Kamaluokeakua | last post by:
I have to write an application that deals with clients in multiple countries. The addresses, phone numbers, country id and currency information has to be stored into a database that allows for the...
2
by: Ori | last post by:
Hi, I'm looking for a good way to validate a US phone number and i though using regular expression for this. I want to support 3 different ways to enter a phone number: 1.Local Phone : 888-8899...
10
by: JackM | last post by:
I'm still working on validating the phone numbers that are entered on a form but have come across a problem I don't understand how to fix. I can handle most instances when it's in regular US...
1
by: venu | last post by:
Hi, I have a different requirement and it is : I need to validate a phone number field. It may or may not be a US phone number. The constraints are : *********************** # It should...
3
by: venu | last post by:
Hi, I have a different requirement and it is : I need to validate a phone number field. It may or may not be a US phone number. The constraints are : *********************** # It should...
2
by: tJ | last post by:
Hi I'd like to validate a string input as a phone number using a regex matcher. This is the criteria: * at least 10 numbers, * only numeric and the following special characters should be...
5
by: Abhishek | last post by:
Hi this is my another validator in javascript to validate the Phone Number :-) <script language='javascript'> function funcCheckPhoneNumber(ctrtxtMobile,e){ if(window.event){ var strkeyIE =...
4
by: luke noob | last post by:
This is my HTML... <head> <script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script> <script type="text/javascript" src="js/script.js"></script> </head> <body>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.