Python-如何使用regex正确解析?它解析除本地格式之外的所有E.164

  • 本文关键字:格式 regex 何使用 Python- python regex
  • 更新时间 :
  • 英文 :


它适用于0032、32、+32,但不适用于0487365060(这是一个有效术语)

  to_user = "0032487365060"
  # ^(?:+|00)(d+)$ Parse the 0032, 32, +32 & 0487365060
  match = re.search(r'^(?:+|00)(d+)$', to_user)
  to_user = "32487365060"
  match = re.search(r'^(?:+|00)(d+)$', to_user)
  to_user = "+32487365060"   
  match = re.search(r'^(?:+|00)(d+)$', to_user)

不工作:

  to_user = "0487365060"   
  match = re.search(r'^(?:+|00)(d+)$', to_user)

您的上一个示例不起作用,因为它既不是以+开头,也不是以00开头。如果这是可选,则需要将其标记为:

r'^(?:+|00)?(d+)$'

请注意,您的第二个示例也不匹配;它也不以CCD_ 3或CCD_ 4开始

演示:

>>> import re
>>> samples = ('0032487365060', '32487365060', '+32487365060', '0487365060')
>>> pattern = re.compile(r'^(?:+|00)?(d+)$')
>>> for sample in samples:
...     match = pattern.search(sample)
...     if match is not None:
...         print 'matched:', match.group(1)
...     else:
...         print 'Sample {} did not match'.format(sample)
... 
matched: 32487365060
matched: 32487365060
matched: 32487365060
matched: 0487365060

考虑到问题和注释,在没有更多信息的情况下(特别是关于必须跟在32部分后面的数字序列的长度,如果它总是32或可能是另一个序列),我最终理解你想要的cab是用获得的:

import re
for to_user in  ("0032487365060",
                 "32487365060",
                 "+32487365060",
                 "0487365060"):  
    m = re.sub('^(?:+32|0032|32|0)(d{9})$','32\1', to_user)
    print m

类似于@eyquem方法,要覆盖从+和00到没有+的所有国际代码,00只适用于比利时,它应该是默认的32+数字:

import re
for to_user in  (# Belgium  
                 "0032487365060",
                 "32487365060",
                 "+32487365060",
                 "0487365060",
                 # USA
                 "0012127773456",
                 "12127773456",
                 "+12127773456",
                 # UK
                 "004412345678", 
                 "4412345678", 
                 "+4412345678"):  
    m = re.sub('^(?:+|00|32|0)(d{9})$','32\1', to_user)
    m = m.replace("+","")
    m = re.sub('^(?:+|00)(d+)$', '\1', m)
    print m

输出:

32487365060
32487365060
32487365060
32487365060
12127773456
12127773456
12127773456
4412345678
4412345678
4412345678

执行成功!

为什么不使用电话号码库

>>> phonenumbers.parse("0487365060", "BE")
PhoneNumber(country_code=32, national_number=487365060, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=0, preferred_domestic_carrier_code=None)

其他3个可以接受

>>> phonenumbers.parse("0032487365060", "BE")
PhoneNumber(country_code=32, national_number=487365060, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=0, preferred_domestic_carrier_code=None)
>>> phonenumbers.parse("+320487365060", "BE")
PhoneNumber(country_code=32, national_number=487365060, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=0, preferred_domestic_carrier_code=None)
>>> phonenumbers.parse("320487365060", "BE")
PhoneNumber(country_code=32, national_number=487365060, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=0, preferred_domestic_carrier_code=None)

最新更新