【问题标题】:Python regex: validate mobile numbers from Germany and AustriaPython regex:验证来自德国和奥地利的手机号码
【发布时间】:2022-01-14 03:49:43
【问题描述】:

我研究过一个匹配德国和奥地利手机号码的正则表达式。我无法完成它。

这是我目前所拥有的:

[^\d]((\+49|0049|0|\+43|0043)\s?(1|9)[1567]\d{1,2}([ \-/]*\d){7,8})(?!\d)

你可以在我的regex-demo查看性能。

  • 国际拨号代码以+49(德国)和+43(奥地利)开头。这包含在我的正则表达式中。
  • 手机号码的长度因您编写电话号码的方式而异。我收集了多个示例demo

问题:如何改进正则表达式以匹配我演示的所有示例?

此外,我想检查特定数字是否与定义的正则表达式匹配。 但是,我的方法并没有真正奏效:

import re
slot_value = "0176 48200179"
regex_mobile = r"[^\d]((\+49|0049|0|\+43|0043)\s?(1|9)[1567]\d{1,2}([ \-/]*\d){7,8})(?!\d)" 

match = re.fullmatch(regex_mobile, slot_value)
print(match)

>>> None

【问题讨论】:

  • 我需要更多解释您的问题。您只想匹配德国和奥地利的号码。对于有效的德国号码:+49、0049、+ (49) 和对于奥地利:+43、0043?对吗?
  • 正确。德国 +49 , 0049, + (49)。奥地利:+43、0043、+ (43)。无国际号码:0
  • 建议先删除括号和东西(规范化字符串),这样既可以使模式更短,也可以让你在解析这些东西时更轻松。

标签: python regex match


【解决方案1】:

在将模式的第一部分与 + 和括号的变体匹配后,一个选项可以断言 10-12 位数字。

请注意,您可以将(1|9) 写为[19],如果您不需要捕获组值,则可以省略括号,并且在模式的开头,您还可以使用字符类将替代项缩短为很喜欢4[39]|004[39]

我以锚点 ^ 开始模式,因为您的模式以 [^\d] 开始,它实际上消耗了一个字符。

^(?:\+4[39]|004[39]|0|\+\(49\)|\(\+49\))\s?(?=(?:[^\d\n]*\d){10,12}(?!\d))(\()?[19][1567]\d{1,2}(?(1)\))\s?\d(?:[ /-]?\d)+

Regex demo

【讨论】:

    【解决方案2】:

    如果要匹配所有数字,当:

    • 德国号码是:+49 , 0049, +(49)
    • 奥地利号码是:+43、0043、+(43)

    您的正则表达式将是:

    .*(?:\+49|0049|\+\(49\)|\+43|0043|\+\(43\)).*
    

    Demo

    您可以使用finditer 查找所有号码。

    下面是一个例子:

    import re
    
    regex = r".*(?:\+49|0049|\+\(49\)|\+43|0043|\+\(43\)).*"
    
    test_str = ("################################### This is allowed ##########################\n"
        "+49 15207930698\n"
        "+49 15207955279\n"
        "+49 1739341284\n"
        "+49 1626589266\n\n"
        "+49915175461907\n"
        "+4915207930698\n"
        "+491635556416\n"
        "017687400179\n"
        "015903900297\n"
        "015175355164\n"
        "015175354885\n"
        "01771789427\n\n"
        "+49 915175461907\n"
        "+43 915175461907\n"
        "+49159039012341\n"
        "+43159039012341\n"
        "+4915207829969\n"
        "+4917697400179\n"
        "+4915903904567\n"
        "+4915902944599\n"
        "+4915902944599\n"
        "+4915903904567\n"
        "+491739341284\n"
        "+431739341284\n\n"
        "+49 176 97 456 123\n"
        "0176 79 123 17 9\n"
        "0176 97 50 01 79\n"
        "0176 79 123 179\n"
        "0174 80123179\n\n"
        "0049 915175461907\n"
        "0043 915175461907\n"
        "0049159039012341\n"
        "0043159039012341\n"
        "004915207829969\n\n"
        "(+49) 17697123456\n"
        "+(49) (1739) 34 12 84\n"
        "+49 (1739) 34-12-84\n\n"
        "############################################################################################\n"
        "################################### This is NOT allowed ####################################\n"
        "012345678901234\n"
        "123w345345345345\n"
        "0123456789101191919\n\n"
        "### Too short\n"
        "+49 15902\n"
        "+49 1590123\n"
        "+49 15903567\n"
        "+49 177178796\n"
        "+49 757130309\n\n"
        "+4915902\n"
        "+491590123\n"
        "+4915903567\n"
        "+49177178796\n"
        "+49757130309\n\n"
        "### Too long\n"
        "+49 1590345985412\n"
        "+491590345985412\n\n"
        "### Not German and not Austrain format\n"
        "+12127319863\n"
        "+13322014056\n"
        "+12126712234\n"
        "+427532697710\n"
        "+417868150810\n"
        "+287533002875\n")
    
    matches = re.finditer(regex, test_str, re.MULTILINE)
    
    for matchNum, match in enumerate(matches, start=1):
        
        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
        
        for groupNum in range(0, len(match.groups())):
            groupNum = groupNum + 1
            
            print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
    

    【讨论】:

    • 非常感谢。但是,我看到您的正则表达式存在 2 个问题:1)它与短到有效的数字相匹配。 2) 与不提供国际码的号码不匹配。这些数字仅以0 开头。我在演示中提供了一些示例。除此之外,您的方法看起来很有希望!
    猜你喜欢
    • 1970-01-01
    • 2015-05-30
    • 2013-07-01
    • 2012-02-01
    • 2013-04-14
    • 2016-10-12
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    相关资源
    最近更新 更多