?
Solved

ASP.NET/VB/REGEX: Custom Formatting for all 10 Digit Phone Numbers

Posted on 2013-01-16
15
Medium Priority
?
501 Views
Last Modified: 2013-05-20
Using ASP.NET / VB and Regular Expressions, I want to format all phone numbers like this:
555.555.1212

If the item contains a comma it should NOT be formatted.

All of these should be changed:
(555) 555-1212
(555)555-1212
(555)5551212
555-5551212
555 555 1212
1 555 555 1212
555-555-1212
1-555-555-1212
1 (555) 555 1212
1 (555) 555-1212
1.555.555.1212
1.555.5551212

These should NOT be changed:
(55) 555-1212
(555) 555-12123
12 555 555 1212
1 (555) 555-CALL
555,555,1212
1,555,555,1212
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38784747
Something like this should correctly format the ones you do want to change:

regex.Replace(regex.Replace(sourcestring,"[^\d,]",""),"^(?!.*,)1?(\d{3})(\d{3})(\d{4})$","$1.$2.$3")

Open in new window


You might need to do something like this (pseudocode) to ensure no other values are changed though:
if (string matches pattern "^(?!.*,)\D*1?(\D*\d){10}\D*$") then
  do the replacement

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38784759
Note that this replacement:
regex.Replace(sourcestring,"[^\d,]","")

Open in new window

simply removes the non-digit characters, so you wouldn't want to run it against the phone numbers that you don't want changed.
0
 
LVL 16

Author Comment

by:hankknight
ID: 38784797
This produces an error:

input = RegularExpressions.Regex.Replace(input,"[^\d,]",""),"^(?!.*,)1?(\d{3})(\d{3})(\d{4})$","$1.$2.$3")

Open in new window


Compiler Error Message: BC30205: End of statement expected.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Terry Woods
ID: 38784812
It should be 2 nested replaces:
RegularExpressions.Regex.Replace(
    RegularExpressions.Regex.Replace(sourcestring,"[^\d,]",""),
    "^(?!.*,)1?(\d{3})(\d{3})(\d{4})$",
    "$1.$2.$3"
)

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 38784895
Thanks, but now it only works if the string contains only 1 phone number.  The other content gets destroyed.  I want it to target the phone numbers within a larger string.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38784948
Try this:

    Dim replacementstring as String = "$1$2$3$4.$5$6$7.$8$9$10$11"
    Dim matchpattern as String = "(\s?)(?!<\d[^a-z\d\n]{0,2})1?[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)(?![a-z\d\n]{0,2}\d)"
    result = regex.Replace(sourcestring,matchpattern,replacementstring,RegexOptions.IgnoreCase))

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38784958
It gets uglier trying to put it all into one replace, which is why I tried to separate it before. Still, it is achievable.
0
 
LVL 16

Author Comment

by:hankknight
ID: 38785026
Now I get a different error.
Dim replacementstring as String = "$1$2$3$4.$5$6$7.$8$9$10$11"
Dim matchpattern as String = "(\s?)(?!<\d[^a-z\d\n]{0,2})1?[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)(?![a-z\d\n]{0,2}\d)"
input = regex.Replace(input,matchpattern,replacementstring,RegexOptions.IgnoreCase))

Open in new window

Compiler Error Message: BC30516: Overload resolution failed because no accessible 'Replace' accepts this number of arguments.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38785089
Is it cause by the extra ) character at the end of the Replace command?
0
 
LVL 16

Author Comment

by:hankknight
ID: 38785209
This also produces an error.  Does it work for you?
Dim replacementstring as String = "$1$2$3$4.$5$6$7.$8$9$10$11"
Dim matchpattern as String = "(\s?)(?!<\d[^a-z\d\n]{0,2})1?[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)(?![a-z\d\n]{0,2}\d)"
input = regex.Replace(input,matchpattern,replacementstring,RegexOptions.IgnoreCase)

Open in new window

BC30516: Overload resolution failed because no accessible 'Replace' accepts this number of arguments.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38785316
I'm a PHP programmer, so I'm working with code generated from myregextester.com, so I haven't tested it I'm afraid. Should it be "RegularExpressions.Regex.Replace" instead of "regex.Replace" maybe?
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 38785339
Could the data type of input be something other than String?

Do you need a line stating:
 Imports System.Text.RegularExpressions

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 38787299
Could you please provide PHP code that does this with preg_replace?

Thanks.
0
 
LVL 16

Author Comment

by:hankknight
ID: 38787550
I got it working by modifying your code.  Thanks for the help!
input = RegularExpressions.Regex.Replace(input, "(\s?)(?!<\d[^a-z\d\n]{0,2})1?[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)[^a-z\n\d,]*(\d)(?![a-z\d\n]{0,2}\d)", "$1$2$3$4.$5$6$7.$8$9$10$11")

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 39181385
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question