1

I am not so into REGEX and I am finsing some problem to adapt this regex that verify a phone number to my use case.

I have this REGEX validating phone number with international prefix: https://www.regextester.com/97440

(([+][(]?[0-9]{1,3}[)]?)|([(]?[0-9]{4}[)]?))\s*[)]?[-\s\.]?[(]?[0-9]{1,3}[)]?([-\s\.]?[0-9]{3})([-\s\.]?[0-9]{3,4})

It correctly validates string as: +39 3298494333 but it doesn't validate string representing a number without the international prefix, for example this string doesn't match my regex 3298494333

How can be changed in order to accept also phone number that doesn't have the prefix?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • That is because if there is no `+` the minimum length is 11 characters – The fourth bird Feb 05 '21 at 19:19
  • @Thefourthbird mmm and can it be changed in order to accept also this type of string? – AndreaNobili Feb 05 '21 at 19:25
  • You could change `[0-9]{4}` to `[0-9]{3}` but I am not sure if that will make it a good pattern. There are a lot of optional things, and it could possibly also match `+1)1)111 1111` Perhaps this page can be helpful https://stackoverflow.com/questions/123559/how-to-validate-phone-numbers-using-regex – The fourth bird Feb 05 '21 at 19:34

1 Answers1

0

Use this fix:

(?:(?:\+\(?[0-9]{1,3}|\(?\b[0-9]{4})\)?)?\s*\)?[-\s.]?\(?[0-9]{1,3}\)?[-\s.]?[0-9]{3}[-\s.]?[0-9]{3,4}

See proof.

Explanation

--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      \+                       '+'
--------------------------------------------------------------------------------
      \(?                      '(' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      [0-9]{1,3}               any character of: '0' to '9' (between
                               1 and 3 times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      \(?                      '(' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w)
                               and something that is not a word char
--------------------------------------------------------------------------------
      [0-9]{4}                 any character of: '0' to '9' (4 times)
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    \)?                      ')' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \)?                      ')' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  \(?                      '(' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [0-9]{1,3}               any character of: '0' to '9' (between 1
                           and 3 times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \)?                      ')' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  [0-9]{3}                 any character of: '0' to '9' (3 times)
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  [0-9]{3,4}               any character of: '0' to '9' (between 3
                           and 4 times (matching the most amount
                           possible))

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37