5

In my exercise I'm supposed to validate phone numbers, which of the correct are :

1234567890
123-456-7890
123.456.7890
(123)456-7890
(123) 456-7890
456-7890

I've tried [(]?[0-9][0-9][0-9][).-]? ?[0-9][0-9][0-9][.-]?[0-9][0-9][0-9][0-9], but it seems it also accepts something like (123.456-7890. How I can handle this? Or I should take completely different way ?

Here are some invalid phone numbers:

123-45-6789
123:4567890
123/456-7890
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
ashur
  • 4,177
  • 14
  • 53
  • 85
  • 3
    You should show incorrect too, otherwise `.*` is a correct answer – Bohemian Jun 25 '13 at 23:07
  • Ah, I've grappled with this problem before. It wouldn't be a bad idea to clearly outline the set of correct and incorrect inputs (for instance, I notice you can mix space and dash seperators, but not dots and brackets?) – Anti Earth Jun 25 '13 at 23:07
  • @ashur, I believe that I meet all of your requirements. I edited my post and tried to explain in as much detail as possible. Hope it's helpful. – Steve P. Jun 25 '13 at 23:59
  • The accepted answer of the Qn your Qn has been marked as a dup of doesn't solve it by regex.Please, have a look at my soln. I had tested all the negative test cases as well that you found passing on other solutions here. – Ravi K Thapliyal Jun 27 '13 at 06:29

3 Answers3

0

Try this one (using look-aheads to match parenthesis and other balance checks):

^(?:((?=\(.*\).*\-)|(?!.*\()(?!.*\)))\(?[0-9]{3}\)?(((?<=[)])|[\-\s])(?=.*\-)|\.(?=.*\.)|(?<=^)|(?=[0-9]+$)))?[0-9]{3}[\s.-]?[0-9]{4}$

Or this one (using \d as this is my contestant to the shortest-regex parade):

^((\d{3}-|\(\d{3}\)\s?)?\d{3}-|^\d{3}(\.)?\d{3}\3)\d{4}$
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • I prefer using `\d` as opposed to `[0-9]`. Nice edit also. – Java Devil Jun 25 '13 at 23:24
  • @JavaDevil Yeah, I do too. The thing is `\d` can match unicode digits as well. If that's not a worry, the regex would certainly look prettier with `\d` :) – acdcjunior Jun 25 '13 at 23:26
  • Wait does this also handle the awkward scenario of `123.456-7890` where the separator between the numbers has to be consistent? I feel that's the challenging aspect of this question... – aug Jun 25 '13 at 23:28
  • Hmmm.. it looks like it also accepts `(123)456.7890` which is not precised to be correct or incorrect in my exercise. – ashur Jun 25 '13 at 23:30
  • @aug There you guys go. Getting prettier and prettier. :) – acdcjunior Jun 25 '13 at 23:34
  • 2
    @acdcjunior - If that's pretty, I'd hate to see ugly... – jahroy Jun 25 '13 at 23:39
  • Not sure if this is allowed for OP but it worked for `123-4567891` and `123456-7891`. I have to say though, you are a sorcerer. http://i.imgur.com/UQ15dEA.png – aug Jun 25 '13 at 23:44
  • Updated! It passes all known tests. @aug, Can you guys check it out? I will leave the explaining part for when it works. – acdcjunior Jun 26 '13 at 00:12
  • @ashur Have you tried this last version? – acdcjunior Jun 26 '13 at 13:19
  • @acdcjunior I've just checked and it does not validate `1234567890`. Thanks for the effort anyway. – ashur Jun 27 '13 at 17:37
  • How are you using them? They do validate `1234567890`. See: http://regexr.com?35ck4 – acdcjunior Jun 27 '13 at 17:42
-1

This pattern works with your examples, i write it without the java escapes:

 ^(?>\(\d{3}\) ?|\d{3}[.-]?)?\d{3}[.-]?\d{4}$

Explanations:

 ^                 begining  of the string
 (?>               open an atomic group
    \(\d{3}\) ?    3 digits with parenthesis followed by a space or not
   |               OR
    \d{3}[.-]      3 digits followed by . or - or not
 )                 close atomic group
 ?                 the atomic group is optional
 \d{3}             3 digits
 [.-]?             followed by a . or a - (optional)
 \d{4}             4 digits
 $                 end of the string         
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
-1

Regex

^\d{10}|^(\(\d{3}\)\s?)?\d{3}-\d{4}$|^\d{3}([.-])\d{3}\2\d{4}$

Matches the following

1234567890
123-456-7890
123.456.7890
(123)456-7890
(123) 456-7890
456-7890

Does not match on

123-45-6789
123:4567890
123/456-7890
(123-456-7890
123.456-7890
(123)456.7890
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89