使用PhoneNumber提取未更改的原始电话号码。PhoneNumberMatcher

2024-09-29 00:16:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从短信中提取电话号码。但我不希望手机被重新格式化,提取号码部分或其他任何东西,但我只希望从文本中提取未更改的原始电话号码字符串,而不是重新格式化的原始字符串。 我用PhoneNumberMatcher()方法尝试了“phonenumbers”库(https://github.com/daviddrysdale/python-phonenumbers)。它工作得很好,在获取格式化电话号码的意义上,这是我不需要的,我只需要电话号码的原始子字符串。有办法吗

for match in phonenumbers.PhoneNumberMatcher(text, "US"):
    unformatted_phone = match.number
    #formatted_phone = str(match.number)
    #formatted_phone = phonenumbers.format_number(match.number, None)
    formatted_phone = phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)

Tags: 方法字符串文本formatnumbermatchphone电话号码