Reputation: 397
I am making a regular expression that will read phone numbers from a PHP form. I have the expression most of the way completed. It needs to read a phone number in any of the following formats:
623-456-7890 456-7890 6234567890 4567890 623.456.7890 456.7890 623 456 7890 456 7890
The expression I have made at this point is the following:
(([0-9]{3}){0,1})((\W){0,1})([0-9]{3})((\W){0,1})([0-9]{4})
It mostly works, the only phone number it doesn't read is the third one in the above list (6234567890)
. What would I have to add or change to make it read that phone number?
Upvotes: 1
Views: 146
Reputation: 4776
/(?:[\(]?\d{3}[\)\.\- ]?)?\d{3}[\.\- ]?\d{4}/
Here it is in practice: http://regex101.com/r/pL3dB0/3
Upvotes: 1
Reputation: 7880
Real phone numbers are much more complicated than this because of exchanges and so forth. This will match numbers, periods and hyphens, not all are required, but it also doesn't check for valid phone numbers.
([0-9]{3})?[ .-]?([0-9]{3})[ .-]?([0-9]{4})
Upvotes: 1