Reputation: 47
I'm trying to get all the phone numbers from a string.
Input string:
Antoine Smith 911 839 7393 9118582588 91145MAURY Bob Smith
I want to get the three phones numbers: 911 839 7393
9118582588
91145MAURY
The regex I'm using.
[a-zA-Z]*([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})+
However this doesn't work.
If I just put in the phrase : "911 839 7393 9118582588 91145MAURY Bob Smith" then the regex returns the phone numbers.
What regex should I be using?
Upvotes: 0
Views: 68
Reputation: 47
Great answers guys.
Used: (0-9?)?((?[0-9]{3})?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})*
Upvotes: 0
Reputation: 173
Interesting... it becomes more challenging when one considers phone numbers by name. So many permutations are possible considering dashes, dots and parenthasis as delimiters. Here is what I came up with:
((\+\d{1,2}\s)?\(?\d{3}\)?[\s\.\-]?\d{3}[\s\.\-]?\d{4})|(\+?\d{1,2}\s?\(?\-?\d{3}\)?\s?\-?[a-zA-Z0-9]{3,4}[\s\.\-]?[a-zA-Z0-9]{3,5}\b)
It seems to work OK for your example and several popular national numbers like 1-800-FLOWERS and 1 800 BEST-BUY, the latter of which was slightly tricky since the pattern is 4 letters - 3 letters instead of the reverse form.
Hope that helps.
Upvotes: 0
Reputation: 31045
You can use this regex:
(\d{3}\s?\d{3}\s?\d{4})|(\d{5}\w{5})
Btw, the regex can be shortened a little to:
((?:\d{3}\s?){2}\d{4})|(\d{5}\w{5})
Upvotes: 0
Reputation: 3690
Assuming that the area code will never be letters and spaces are the only punctuation to account for, this will work \d{3} ?\w{3} ?\w{4}
.
Upvotes: 2