473,761 Members | 10,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(C onvert.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 4545
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.substrin g(0,3) & ") " & number.substrin g("3,3) & "-"
& number.substrin g(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******** ********@TK2MSF TNGP14.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(C onvert.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*********@ms n.com> wrote in message
news:ef******** *****@tk2msftng p13.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.substrin g(0,3) & ") " & number.substrin g("3,3) &
"-" & number.substrin g(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******** ********@TK2MSF TNGP14.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(C onvert.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:

abc123def456ghi 7890
(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 CleanPhoneNumbe r(ByRef value As String)
value = _cleanPhone.Rep lace(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").ToS tring()
phone = tx.CleanPhoneNu mber(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*****@hotmai l.com> wrote in message
news:ep******** ******@TK2MSFTN GP15.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 CleanPhoneNumbe r(ByRef value As String)
value = _cleanPhone.Rep lace(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").ToS tring()
phone = tx.CleanPhoneNu mber(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
19232
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 pretty limited. I wrote the following expression to take (hopefully) any _reasonable_ phone number input, and format it as (999) 999-9999 x 9999.
5
3523
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 format of any country. I would like to be able to automatically display the information in the format of their country based on the information gathered from the Country field in the database. Is there any white paper, or book or site that has...
2
4014
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 2.With extension: (310)888-2569 3.With extension + 1 : 1(888)789-2569 Can someone tell me what expression I can us in order to support those
10
14303
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 formats (555-555-5555 or (555) 555-5555 or 555 555 5555) but I'm having trouble when the entry is ten consecutive numbers with nothing else (5555555555). Not only does it not validate, but it shows as 2147483647 when I call the variable to check to see...
1
2126
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 accept any number of numbers
3
6790
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 accept any number of numbers
2
6114
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 allowed: '+' '-' '(' ')' and spaces The closest I've been able to find is ^*$ :-(
5
3683
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 = e.keyCode if(((strkeyIE >= 48) && (strkeyIE <= 57 )) || (strkeyIE >= 40) && (strkeyIE <= 41 ) || (strkeyIE == 32) || (strkeyIE == 46)||(strkeyIE
4
5059
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
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.