简体   繁体   English

电话号码正则表达式

[英]telephone number regex

I am currently trying to validate UK telephone numbers:我目前正在尝试验证英国电话号码:

The format I'm looking for is: 01234 567891 or 01234567891 - So I need the number to have 5 numbers then a space then 6 numbers or simply a 11 numbers.我正在寻找的格式是:01234 567891 或 01234567891 - 所以我需要这个数字有 5 个数字,然后是一个空格,然后是 6 个数字,或者只是 11 个数字。

The number must start with a 0.数字必须以 0 开头。

I've had a look at a couple of examples:我看过几个例子:

/^[0-9]{10,11} - to check that the chars are all numbers /^0[0-9]{9,10}$/ - to check that the first number is a 0 /^[0-9]{10,11} - 检查字符是否都是数字/^0[0-9]{9,10}$/ - 检查第一个数字是否为 0

I'm just unsure how to put all these together and check if there is a space or not.我只是不确定如何将所有这些放在一起并检查是否有空格。

Could someone help me with this regex?有人可以帮我解决这个正则表达式吗?

Thanks谢谢

Try this regex:试试这个正则表达式:

/^0\d{4}\s?\d{6}$/ 

Many people try to do input validation and formatting in a single step.许多人尝试一步完成输入验证和格式化。

It is better to separate these processes.最好将这些过程分开。

Match UK telephone number in any format匹配任何格式的英国电话号码

^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$ 

The above pattern allows the user to enter the number in any format they are comfortable with.上述模式允许用户以他们喜欢的任何格式输入数字。 Don't constrain the user into entering specific formats.不要限制用户输入特定的格式。

Extract NSN, prefix and extension提取 NSN、前缀和扩展名

^(\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)(44)\)?[\s-]?)?\(?0?(?:\)[\s-]?)?([1-9]\d{1,4}\)?[\d[\s-]]+)((?:x|ext\.?|\#)\d{3,4})?$

Next, extract the various elements.接下来,提取各种元素。

$2 will be '44' if international format was used, otherwise assume national format with leading '0'.如果使用国际格式,$2 将是“44”,否则假定使用前导“0”的国家格式。

$4 contains the extension number if present. $4 包含分机号码(如果存在)。

$3 contains the NSN part. $3 包含 NSN 部分。

Validation and formatting验证和格式化

Use further RegEx patterns to check the NSN has the right number of digits for this number range.使用进一步的 RegEx 模式来检查 NSN 是否具有此数字范围的正确位数。 Finally, store the number in E.164 format or display it in E.123 format.最后,将号码以 E.164 格式存储或以 E.123 格式显示。

There's a very detailed list of validation and display formatting RegEx patterns for UK numbers at:有一个非常详细的英国号码验证和显示格式 RegEx 模式列表:

http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers

It's too long to reproduce here and it would be difficult to maintain multiple copies of this document.在这里复制太长了,并且很难维护该文档的多个副本。

/\d*(*)*+*-*/ /\d*(*)*+*-*/

Simple Telephone Regex includes + () and - anywhere, as well as digits简单的电话正则表达式包括 + () 和 - 任何地方,以及数字

If you are looking for all UK numbers, I'd look for a bit more than just that number, some are in the format 020 7123 4567 etc.如果您正在寻找所有英国号码,我会寻找的不仅仅是那个号码,有些是 020 7123 4567 等格式。

^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$

This US numbers pattern accepts following phones as well:这种美国号码模式也接受以下电话:

800-432-4500, Opt: 9, Ext: 100316 800-432-4500,选项:9,分机:100316
800-432-4500, Opt: 9, Ext: X100316 800-432-4500,选项:9,分机:X100316
800-432-4500, Option #3 800-432-4500,选项 #3

(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4}),?(?:\s*(?:#|x\.?|opt(\.|:|\.:)?|option)\s*#?(\d+))?,?(?:\s*(?:#|x\.?|ext(\.|:|\.:)?|extension)\s*(\d+))?

(used this answer in other topic as start point) 在其他主题中使用此答案作为起点)

I think ^0[\d]{4}\s?[\d]{5,6}} will work for you.我认为^0[\d]{4}\s?[\d]{5,6}}会为你工作。 I have used [\d] instead of [0-9] .我使用[\d]而不是[0-9] I find that RegExr is a useful online tool to check and try your regular expressions.我发现RegExr是一个有用的在线工具,可以检查和尝试您的正则表达式。 It also has a nice library of examples to help point you in the right direction它还有一个很好的示例库,可帮助您指明正确的方向

you should just count the number of digits and check that it's 10,你应该只计算位数并检查它是10,

Some UK numbers have only 9 digits, not 10 (not including the leading 0).一些英国号码只有 9 位数字,而不是 10 位(不包括前导 0)。

These include 40 of the 01 area codes (using "4+5" format), the 016977 area code (using "5+4" format), all 0500 numbers and some 0800 numbers.其中包括 01 区号中的 40 个(使用“4+5”格式)、016977 区号(使用“5+4”格式)、所有 0500 号码和一些 0800 号码。

There's a list at: http://www.aa-asterisk.org.uk/index.php/01_numbers有一个列表: http://www.aa-asterisk.org.uk/index.php/01_numbers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM