Solved

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

Posted on 2013-01-16
15
496 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…

777 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