tradeline
asked on
C# Regex Phone Number
I'm looking for a comprehensive Regex pattern for a north american phone number. Regex isn't my strong suit so I thought I'd put it up here as I'm sure it's a common problem. The main thing is that I want to capture anything that resembles a phone number. It's only used to highlight suspected phone number in text for the user, so false positives are less worrysome than missed numbers...
The following entities should match:
(###)-###-####
(###).###.####
###-###-####
###.###.####
### ### ####
(###) ### ####
### ###-####
### ###.####
etc...
Basically, three number area code (optionally surrounded by parenthesis), with 7 digits following the area code. Number groups can be delimited by spaces, dashes or dots, and shouldn't need to be consistent. For example: 604 555-5555 or 604 555.5555 or (604)-555-5555 etc...
Thanks for your help.
sk
The following entities should match:
(###)-###-####
(###).###.####
###-###-####
###.###.####
### ### ####
(###) ### ####
### ###-####
### ###.####
etc...
Basically, three number area code (optionally surrounded by parenthesis), with 7 digits following the area code. Number groups can be delimited by spaces, dashes or dots, and shouldn't need to be consistent. For example: 604 555-5555 or 604 555.5555 or (604)-555-5555 etc...
Thanks for your help.
sk
\b(\(\d{3}\)|\d{3})([-.]|\ s+)?\d{3}( [-.]|\s+)? \d{3}\b
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ozo..at your solution 604 555-5555, as in the solution above, would not work
"shouldn't need to be consistent"
"shouldn't need to be consistent"
Sorry, I meant
\b(\(\d{3}\)|\d{3})([-.]|\ s+)?\d{3}( [-.]|\s+)? \d{4}\b
\b(\(\d{3}\)|\d{3})([-.]|\
(\(\d{3}\)|\b\d{3})([-.]|\ s+)?\d{3}( [-.]|\s+)? \d{4}\b
ASKER
Thanks guys, one last thing, in order to match the phonenumber inside of any other given string, I assume that I can add an asterix to the beginning and end of the pattern?
For example, dkloeck's solution: ^(\(\d{3}\)|\d{3})(\.| |\-)\d{3}(\.| |\-)\d{4}$ changes to ??
Gracias.
For example, dkloeck's solution: ^(\(\d{3}\)|\d{3})(\.| |\-)\d{3}(\.| |\-)\d{4}$ changes to ??
Gracias.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.