Title |
Test
Find
Pattern Title
|
Expression |
^[^0-9]*(?:(\d)[^0-9]*){10}$ |
Description |
Recently I've been forced to validate phone numbers in a variety of formats, with all sorts of whitespace, dashes, parenthesis, etc. I didn't want to hassel with matching all that junk, I just wanted the 10 numbers!
So I wrote this expression which captures 10 digits in a string if there are exactly 10 digits. Anything else is allowed so long as there are 10 numbers.
In .Net, You just loop through the Captures collection of Groups[1] of your Regex.Match object and concatenate the Capture.Value strings to get a simple, pure 10 digit phone number. ( myMatch.Groups[1].Captures ) |
Matches |
1234567890 | 585-737-8899 | (324)blah225-foo2587 |
Non-Matches |
1234567 | 12345678901 |
Author |
Rating:
Not yet rated.
Todd Moon
|
Source |
|
Your Rating |
|
Title: Perfect!
Name: Jeffrey Roy
Date: 9/8/2007 11:28:29 PM
Comment:
I don't use these enough to pull this off without a couple hours of research and testing. Who cares what else is in there--I'm going to format it the way I want anyway (i.e. consistently with all other numbers). We leave the formatting to the GUI and the database just gets the digits. Thanks, Todd!