Description: Regular expression for validating US telephone numbers with OPTIONAL area code. Matches various permutations of formatting characters (parenthesis, space, dash). Parses the telephone number area code, prefix, and suffix to named groups to facilitate program manipulation. Area code is optional and can optionally be enclosed in parentheses. Rejects area codes that begin with 0 or 1 and prefixes that begin with 0. Rejects all telephone numbers that do not match on exactly 7 digits, or on exactly 10 digits with the optional area code, not counting the formatting characters. Matches: [333-4444], [222 333 4444], [(222) 333-4444] Non-Matches: [222333 4444], [222-333 4444], [(222)-333 4444]
Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Joined: Oct 12, 2000
Of course that's a completely invalid regexp to validate phone numbers in just about any other country...
Dutch numbers for example contain usually 7 digits and an optional 3 digit area code (the first number of which must be a zero), but there are exceptions where the number is 6 digits and a 4 digit area code. But other numbers are possible. Take mobile phones which here are 10 digits, with the first two always being 06. Or take 0800 and 090x numbers which can be as short as 4 digits (plus the 0800/090x) or as long as over a dozen. Now if you want to allow historical numbers as well, it gets even more interesting (as our numbering system changed dramatically in the 1980s and again in the 1990s)
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: May 06, 2004
It also needs to be mentioned that that regex uses conditionals and named groups--two features not supported by the jdk1.4 regex engine.
And, of course, you have to turn smilies off if you want to post regexes.
BTW, don't regex questions belong in the Intermediate forum?