Описание
Вы предоставили следующие примеры номеров, которые вы хотели бы согласовать.
+4454475294x364
+44 544-75294 x364
(123) 555-1212x4567
123-555-1232
Regex
Это регулярное выражение будет делать следующее:
- Матча международных номеров формата вы предоставили
- Match североамериканских номера
- Если телефонный номер за которым следует расширение, затем запишите его
- Разрешить пробелы, дефис и скобки в очевидных местах
- Это ограничивается только форматы, которые вы перечислили в своем вопросе
^(?:[+][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]+)?
Примечание: для Java вам нужно бежать косые черты \
выглядеть как \\
.
Пояснение
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
Примеры
Используя образец текста выше
спичек
[0][0] = +4454475294x364
[1][0] = +44 544-75294 x364
[2][0] = (123) 555-1212x4567
[3][0] = 123-555-1232
Вы должны отличить валидацию для телефонов в Северной Америке и остальном мире. По историческим причинам номера телефонов разные. – MaxZoom