Solved

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

Posted on 2013-01-16
15
497 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

860 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