Italian phone 10-digit number regex issue

The following regex appears to fulfill your requirements. I took out the syntax errors and guessed a bit, and added the missing parts to cover your TODO comments.

^(\((00|\+)39\)|(00|\+)39)?(38[890]|34[7-90]|36[680]|33[3-90]|32[89])\d{7}$

Demo: https://regex101.com/r/yF7bZ0/1

Your test cases fail to cover many of the variations captured by the regex; perhaps you'll want to beef up the test set to make sure it does what you want.

The beginning allows for an optional international prefix with or without the parentheses. The basic pattern is (00|\+)39 and it is repeated with or without parentheses around it. (Perhaps a better overall approach would be to trim parentheses and punctuation as well as whitespace before processing begins; you'll want to keep the plus as significant, of course.)

Updated with information from @Edoardo's answer; wrapped for legibility and added comments:

^                           # beginning of line
(\((00|\+)39\)|(00|\+)39)?  # country code or trunk code, with or without parentheses
(                           # followed by one of the following
 32[89]|                    # 328 or 329
 33[013-9]|                 # 33x where x != 2
 34[04-9]|                  # 34x where x not in 1,2,3
 35[01]|                    # 350 or 351
 36[068]|                   # 360 or 366 or 368
 37[019]                    # 370 or 371 or 379
 38[089])                   # 380 or 388 or 389
\d{6,7}                     # ... followed by 6 or 7 digits
$                           # and end of line

There are obvious accidental gaps which will probably also get filled over time. Generalizing this further is likely to improve resilience toward future changes, but of course may at the same time increase the risk of false positives. Make up your mind about which is worse.


I found this and i updated with new operators and MVNO prefixes (Iliad, ho.)

^(\((00|\+)39\)|(00|\+)39)?(38[890]|34[4-90]|36[680]|33[13-90]|32[89]|35[01]|37[019])\d{6,7}$

Tags:

Regex