hankknight
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
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
Note that this replacement:
regex.Replace(sourcestring,"[^\d,]","")
simply removes the non-digit characters, so you wouldn't want to run it against the phone numbers that you don't want changed.
ASKER
This produces an error:
Compiler Error Message: BC30205: End of statement expected.
input = RegularExpressions.Regex.Replace(input,"[^\d,]",""),"^(?!.*,)1?(\d{3})(\d{3})(\d{4})$","$1.$2.$3")
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"
)
ASKER
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))
It gets uglier trying to put it all into one replace, which is why I tried to separate it before. Still, it is achievable.
ASKER
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))
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?
ASKER
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)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Could you please provide PHP code that does this with preg_replace?
Thanks.
Thanks.
ASKER
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")
ASKER
I asked a related question here:
https://www.experts-exchange.com/questions/28133331/ASP-NET-VB-REGEX-Leading-Stuff-Zapped.html
https://www.experts-exchange.com/questions/28133331/ASP-NET-VB-REGEX-Leading-Stuff-Zapped.html
Open in new window
You might need to do something like this (pseudocode) to ensure no other values are changed though:
Open in new window