正则表达与荷兰电话号码

时间:2014-01-07 10:01:10

标签: java regex phone-number

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

[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。

1 个答案:

答案 0 :(得分:3)

我曾经在python中写过一个与国际电话号码匹配的phonenumber正则表达式,但对荷兰语电话号码有特殊支持。也许这可以帮助你创建自己的正则表达式。

如果要运行此代码段,请注意此函数使用您应安装的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)