472,958 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 4476
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>
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.