简体   繁体   English

使用re从文字中提取电话号码?

[英]Extract phone number from text using re?

I need to extract phone number, but my regex don't extract all numbers 我需要提取电话号码,但我的正则表达式不会提取所有电话号码

text = '+79082343434   8(912)2342554,  +7 982 342 sdfdsf 34 34 fsf 8-923-132-34-23 +7 982 342 34 34! sdfsd'
r = re.compile(r"\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d")
phone = r.findall(text)

Out[7]: ['+79082343434', '8(912)2342554']

Desire output: 需求输出:

['+79082343434', '8(912)2342554', '8-923-132-34-23', '+7 982 342 34 34']

How can I improve this expression to get all phone numbers? 如何改进此表达式以获取所有电话号码?

This will give you the output you want: 这将为您提供所需的输出:

text = '+79082343434   8(912)2342554,  +7 982 342 sdfdsf 34 34 fsf 8-923-132-34-23 +7 982 342 34 34! sdfsd'
regex = re.compile("\+?\d[\( -]?\d{3}[\) -]?\d{3}[ -]?\d{2}[ -]?\d{2}")
numbers = re.findall(regex, text)

But you might need to fine tune the expression a bit, depending on exactly what you want to consider a phone number. 但是您可能需要稍微调整一下表达式,具体取决于您要考虑的电话号码。

This should find all the phone numbers in a given string 这应该找到给定字符串中的所有电话号码

re.findall(r'+?(?[1-9][0-9 .-()]{8,}[0-9]', text) re.findall(r'+?(?[1-9] [0-9 .-()] {8,} [0-9]',文本)

 >>> re.findall(r'[\+\(]?[1-9][0-9 .\-\(\)]{8,}[0-9]', text)
 ['+79082343434   8(912)2342554', '8-923-132-34-23', '+7 982 342 34 34']

Basically, the regex lays out these rules 基本上,正则表达式列出了这些规则

  1. The matched string may start with + or ( symbol 匹配的字符串可以以+或(符号开头
  2. It has to be followed by a number between 1-9 它后面必须跟一个1-9之间的数字
  3. It has to end with a number between 0-9 它必须以0-9之间的数字结尾
  4. It may contain 0-9 (space) .-() in the middle. 它的中间可能包含0-9(空格).-()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM