Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

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

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
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

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

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.
Avatar of hankknight

ASKER

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

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

It gets uglier trying to put it all into one replace, which is why I tried to separate it before. Still, it is achievable.
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.
Is it cause by the extra ) character at the end of the Replace command?
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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Could you please provide PHP code that does this with preg_replace?

Thanks.
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