gpt4 book ai didi

java - 带有荷兰电话号码的正则表达式

转载 作者:行者123 更新时间:2023-11-30 08:27:15 24 4
gpt4 key购买 nike

我需要为荷兰电话号码创建一个正则表达式。到目前为止我得到了什么:

[0-9\\-\\+]{8,14}

该正则表达式非常适合正常数字,例如:020-5951611

但是我需要添加服务号码,例如“112”、“0800-1234”,但“0909-12345678”必须无效,因为它太长了。我有一个包含大量测试用例的文件(在此处),如果为真,则数字应该有效,如果为假,则数字无效。

    System.err.println("running checkNummer");
TelefoonNummerChecker instance = new TelefoonNummerChecker();
assertEquals(true, instance.checkNummer("020-5951611"));
assertEquals(true, instance.checkNummer("0205951611"));
assertEquals(true, instance.checkNummer("+31205951611"));
assertEquals(true, instance.checkNummer("+3120-5951611"));
assertEquals(true, instance.checkNummer("+31612345678"));
assertEquals(true, instance.checkNummer("+316-12345678"));
assertEquals(true, instance.checkNummer("06-12345678"));
assertEquals(true, instance.checkNummer("0612345678"));
assertEquals(true, instance.checkNummer("0031612345678"));
assertEquals(true, instance.checkNummer("00316-12345678"));
assertEquals(true, instance.checkNummer("0234-123456"));
assertEquals(true, instance.checkNummer("0234123456"));
assertEquals(true, instance.checkNummer("+31234-123456"));
assertEquals(true, instance.checkNummer("+31234123456"));
assertEquals(true, instance.checkNummer("+31320123456")); // lelystad 0320
assertEquals(true, instance.checkNummer("0031320123456"));
assertEquals(true, instance.checkNummer("0031320-123456"));
assertEquals(true, instance.checkNummer("0320-123456"));
// korte service nummers:
assertEquals(true, instance.checkNummer("112"));
assertEquals(true, instance.checkNummer("0800-1234"));
assertEquals(false, instance.checkNummer("0909-12345678")); // too long
assertEquals(true, instance.checkNummer("09001234"));
assertEquals(true, instance.checkNummer("08009876"));
assertEquals(false, instance.checkNummer("0808-123456"));
assertEquals(false, instance.checkNummer("+32205951611")); // not valid in NL
assertEquals(false, instance.checkNummer("+3220-5951611")); // not valid in NL
assertEquals(false, instance.checkNummer("02-05951611")); // before dash should be 3 or 4 long
assertEquals(false, instance.checkNummer("02-15951611")); // before dash should be 3 or 4 long
assertEquals(false, instance.checkNummer("0-215951611")); // before dash should be 3 or 4 long
assertEquals(false, instance.checkNummer("02059-51611")); // before dash should be 3 or 4 long
assertEquals(false, instance.checkNummer("0034-1234567")); // before dash may not have 2x a 0
assertEquals(false, instance.checkNummer("0200951611")); // may not start with 0
assertEquals(false, instance.checkNummer("02059516229")); // too long
assertEquals(false, instance.checkNummer("020595162")); // too short
assertEquals(false, instance.checkNummer("020-0234567")); // may not start with 0
assertEquals(false, instance.checkNummer("123456789")); // no 10 numbers
assertEquals(false, instance.checkNummer("1234567890")); // net number doesn't start with 0
assertEquals(false, instance.checkNummer("123-5951619")); // net number doesn't start with 0
assertEquals(false, instance.checkNummer("0320-012345")); // abo starts with zero

assertEquals(false, instance.checkNummer("+31-205951611")); // no dash before abon number
assertEquals(false, instance.checkNummer("+31-2-05951611")); // no 2x -
assertEquals(false, instance.checkNummer("+31-2-0-5951611")); // no 2x -
assertEquals(false, instance.checkNummer("+31-2-0-595161-1")); // no 2x -
assertEquals(false, instance.checkNummer("06-1-2345678")); // no 2x -
assertEquals(false, instance.checkNummer("020-59-51611")); // no 2x -
assertEquals(false, instance.checkNummer("020-5-9-51611")); // no 2x -
assertEquals(false, instance.checkNummer("0205951-611")); // too long
assertEquals(false, instance.checkNummer("06-1-2345678")); // no 2x -
assertEquals(false, instance.checkNummer("0045106539985")); // not dutch
assertEquals(false, instance.checkNummer("0031476534")); // 0031, not enough numbers next to 0031
assertEquals(false, instance.checkNummer("+310612345678")); // 06 must be 6
assertEquals(false, instance.checkNummer("00310205951688")); // 020 must be 20
System.err.println("end test checkNummer");

我希望有人能帮我解决这个问题,在此先感谢!

编辑:它应该是用 Java 编写的。

最佳答案

我曾经用 python 编写了一个电话号码正则表达式,它匹配国际电话号码,但特别支持荷兰电话号码。也许这可以帮助您创建自己的正则表达式。

如果要运行此代码段,请注意此函数使用 python regex您应该安装以便使用它的模块。

import regex


def phone_regex():
"""
Matches phone numbers with formatting like spaces, parentheses and
dashes.

Has special checkig for dutch phone numbers, but supports international
phone numbers too.

more info:
https://en.wikipedia.org/wiki/List_of_international_call_prefixes
https://nl.wikipedia.org/wiki/Telefoonnummer
https://nl.wikipedia.org/wiki/Informatienummer
"""

# a number with possibly a dash prepended
num_dash = '(?:-\s*)?[0-9]\s*'

return regex.compile(r'''
^(?:

\s*

# Dutch phone number:

(?:

#regular numbers:

(?:
# a zero followed by a a nonzero number
0\s*[1-9]\s*
(?:''' + num_dash + '''){8}
|
# 3 digit area code in parentheses
\(0\s*[1-9]\s*[0-9]\s*\)\s*
(?:''' + num_dash + '''){7}
|
# 4 digit area code in parentheses
\(0\s*[1-9]\s*(?:[0-9]\s*){2}\)\s*
(?:''' + num_dash + '''){6}
)

|

# infonumbers:

(?:
# 0900 number
0\s*9\s*0\s*0\s*
|
# 0800 number
0\s*8\s*0\s*0\s*
|
# 0906 number
0\s*9\s*0\s*6\s*
|
# 0909 number
0\s*9\s*0\s*9\s*
)
(?:''' + num_dash + '''){4,7}

|

# 112 alarm number
1\s*1\s*2\s*

)

|

# International phone number:

# plus sign, to indicate international phone number
\+\s*
# the country code, can be 1 to 4 characters
(?:[0-9]\s*){1,4}

# area code, can be a number from 1 to 4 digits
(?:
# area code in parentheses
\((?:[0-9]\s*){1,4}\)\s*
|
# area code without parentheses
(?:[0-9]\s*){1,4}
)

# local number
(?:''' + num_dash + '''){5,9}

)$''', regex.X)

关于java - 带有荷兰电话号码的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20968700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com