The following regular expression:
(\([2-9]\d\d\)|[2-9]\d\d) ?[-.,]? ?[2-9]\d\d ?[-.,]? ?\d{4}
matches all of the following:
860-678-2345
(860) 678-2345
(860) 678 - 2345
and probably a fair amount else too. Broken down:
(\([2-9]\d\d\)|[2-9]\d\d)
- Matches the first part of the number with or without brackets?[-.,]? ?
- A hyphen, period (or full stop to us Brits) or a comma, with or without surrounding spaces.[2-9]\d\d
- Matches the second part of the number.\d{4}
- Matches the final part of the number.
\d\d
and [0-9]{2}
are equivalent; the former is just slightly shorter so improves readability. Likewise, [2-9]
and [2-9]{1}
are equivalent; the {1}
just means "one instance of the preceeding pattern", which is a given anyway.