简体   繁体   English

用于澳大利亚电话号码验证的正则表达式

[英]Regex for Australian phone number validation

I tried below regex for phone numbers validation but did not work properly,我尝试在正则表达式下进行电话号码验证,但没有正常工作,

^([+61|0](2|4|3|7|8|)){0,2}([ 0-9]|[(]){2,3}([)]|[0-9]){6}([ ])[0-9]{7,20}$

Here are examples I expect to pass validation以下是我希望通过验证的示例

+61(02)89876544
+61 2 8986 6544
02 8986 6544
+61289876544
0414 570776 
0414570776
0414 570 776
+61 414 570776
+61 (0)414 570776

below characters also be accepted.也接受以下字符。 ()-+ space ()-+ 空格

That's quite the phone number structure. 那就是电话号码结构。 The easiest solution is to ask the user to input in a particular format or have the the form automatically format it. 最简单的解决方案是要求用户以特定格式输入或让表单自动格式化。 If you must use a regex, this is what I came up with: 如果你必须使用正则表达式,这就是我提出的:

^(?:\+?(61))? ?(?:\((?=.*\)))?(0?[2-57-8])\)? ?(\d\d(?:[- ](?=\d{3})|(?!\d\d[- ]?\d[- ]))\d\d[- ]?\d[- ]?\d{3})$

Capture groups: 捕获组:

  1. Country Code (optional) 国家代码(可选)
  2. Area/Provider Code (optional) 区域/提供者代码(可选)
  3. The Phone Number 电话号码

Demo: https://regex101.com/r/dkFASs/6 演示: https//regex101.com/r/dkFASs/6

This was what I used to nail down the format: https://en.wikipedia.org/wiki/Telephone_numbers_in_Australia 这就是我过去常用的格式: https//en.wikipedia.org/wiki/Telephone_numbers_in_Australia

When tackling a larger regex like this, I find the esiest way is to stick your test cases into a tool like regex101.com and then go capture group by capture group (naming them if you can) until you get to the end. 在处理这样一个更大的正则表达式时,我发现最常用的方法是将测试用例放到像regex101.com这样的工具中,然后通过捕获组捕获组(如果可以,则命名它们),直到结束。 As soon as one group doesn't match, change what you already have until it does. 一旦一组不匹配,就改变你已有的组,直到它完成。

Edit 2019: Since this question still gets attention occasionally, I wanted to note that it might be better to use a validation library if possible. 编辑2019:由于这个问题偶尔会引起注意,我想要注意,如果可能的话,最好使用验证库。 My suggestion would be Google's i18n library for phone numbers . 我的建议是谷歌的i18n电话号码库 It's much more robust than a regex could ever be. 它比正则表达式更强大。

I would first remove all whitespace from the phone number as more patterns will mean more complex regex, then apply the following: 我会首先从电话号码中删除所有空格,因为更多模式将意味着更复杂的正则表达式,然后应用以下内容:

(?:\+?61)?(?:\(0\)[23478]|\(?0?[23478]\)?)\d{8}

As far as I can tell it works. 据我所知,它有效。

Will write explanation shortly 将尽快写下解释

I up-voted @3ocene's response. 我对@ 3ocene的回复进行了投票。 But later found didn't quite match my case. 但后来发现并不符合我的情况。

Here's my test case (non exhaustive): 这是我的测试用例(非详尽):

+(61) 455 562 400
+61-455-562-400
+61 455 562 400
+(61)-455-562-400
+(61) 455 562 400
(02) 4371 3164
(02) 4371-3164
02 80268989
03 80268989
04 80268989
05 80268989
433245898
433 245 898
433-245-898
4555-62400
123456
08 54 587 456

123456 is negative.. 123456是否定的..

This is what I built, so it it helps someone here it is: 这就是我建立的,所以它可以帮助这里的人:

(\(+61\)|\+61|\(0[1-9]\)|0[1-9])?( ?-?[0-9]){6,9}

Here's the Regexer link: 这是Regexer链接:

https://regex101.com/r/eiufOH/2 https://regex101.com/r/eiufOH/2

尝试这个

\b([\+-]?\d{2}|\d{4})\s*\(?\d+\)?\s*(?:\d+\s*)+\b

Modified SydMK to include 13, 180, 19, 1300, 1800, 1900修改 SydMK 以包括 13、180、19、1300、1800、1900

13 xxxx and 1300 xxx xxx – "Local Rate" calls, except for VoIP and mobile phone users 13 xxxx 和 1300 xxx xxx – “本地费率”呼叫,VoIP 和移动电话用户除外

180 xxxx and 1800 xxx xxx – FreeCall 180 xxxx 和 1800 xxx xxx – FreeCall

19 xxxx and 19xx xxxx – Premium SMS 19 xxxx 和 19xx xxxx – 高级短信

^(\+?\(61\)|\(\+?61\)|\+?61|1[ \-]{0,1}(3|80|9|300|800|900)|\(0[\d]\)|0[\d])?(( ?-?[\d]){8,9}|[\d]{4})$

https://regex101.com/r/dqsaHM/1 https://regex101.com/r/dqsaHM/1

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

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