-3

I am using "^[(\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?]$" regex to validate phone number. I want it to work for international numbers as well. It is working for the patterns: +4454475294x364

I want to add space and '-' also. example: +44 544-75294 x364.

What changes I need more in my regex.

Thanks.

user1915636
  • 139
  • 1
  • 3
  • 16
  • You have to distinguish the validation for phones in Northen America and rest of the world. Per historical reasons the phone numbers are different. – MaxZoom May 02 '16 at 21:20

1 Answers1

3

Description

You provided the following examples of numbers you'd like matched.

+4454475294x364
+44 544-75294 x364
(123) 555-1212x4567
123-555-1232

The Regex

This regex will do the following:

  • Match international numbers of the format you provided
  • Match North American numbers
  • If the phone number is followed by an extension, then capture that
  • Allow spaces, hyphens, and parentheses at obvious spots
  • This is limited to just the formats that you listed in your question

^(?:[+][0-9]{2}\s?[0-9]{3}[-]?[0-9]{3,}|(?:[(][0-9]{3}[)]|[0-9]{3})\s*[-]?\s*[0-9]{3}[-][0-9]{4})(?:\s*x\s*[0-9]+)?

Note: for Java you'll need to escape the forward slashes \ to look like \\.

Explanation

Regular expression visualization

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [+]                      any character of: '+'
----------------------------------------------------------------------
    [0-9]{2}                 any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
    \s?                      whitespace (\n, \r, \t, \f, and " ")
                             (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
    [-]?                     any character of: '-' (optional
                             (matching the most amount possible))
----------------------------------------------------------------------
    [0-9]{3,}                any character of: '0' to '9' (at least 3
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      [(]                      any character of: '('
----------------------------------------------------------------------
      [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
      [)]                      any character of: ')'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [-]?                     any character of: '-' (optional
                             (matching the most amount possible))
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
    [-]                      any character of: '-'
----------------------------------------------------------------------
    [0-9]{4}                 any character of: '0' to '9' (4 times)
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    x                        'x'
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )?                       end of grouping

Examples

Using the sample text above

Matches

[0][0] = +4454475294x364
[1][0] = +44 544-75294 x364
[2][0] = (123) 555-1212x4567
[3][0] = 123-555-1232
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43