Solved

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

Posted on 2013-01-16
15
494 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now