简体   繁体   English

使用Python正则表达式使用连字符提取电话号码

[英]Extract phone numbers with hyphen using Python regex

I have an address string like this 我有这样的地址字符串

addr_str = "No 123 4th St, 5th Ave NYC\n\tPhone: 9938483902"

Currently, I'm using regex to extract phone number from the end of the string like this: 当前,我正在使用正则表达式从字符串末尾提取phone number ,如下所示:

phone = re.search(r'\d+$', addr_str)
print phone.group()

I just realized that there are some phone numbers like: 我刚意识到有一些电话号码,例如:

040-38488993 
3888-32888222 
01854-29924402

How can I alter this regex to get the numbers before the hyphen? 如何更改此正则表达式以获取连字符前的数字? Any help? 有什么帮助吗?

Please note that the number of digits before the hyphen vary erratically and I also have numbers without any hyphens which I need as well. 请注意,连字符前的位数不规则地变化,我也有不带连字符的数字。

Just put - , \\d inside a char class. 只需将-\\d放在char类中即可。

phone = re.search(r'[\d-]+$', addr_str)

If the phonenumber startswith with a optional + then you may try this, 如果电话号码以可选的+开头,那么您可以尝试此操作,

phone = re.search(r'\+?\d+(?-\d+)*$', addr_str)
phone = re.search(r'\d[\d-]+\d$', addr_str)

你可以简单地修改你的正则表达式来this.If总是有只有1成为了可能-使用

phone = re.search(r'\d+-\d+$', addr_str)

您可以让数字模式包含可选的减号,并期望该组重复1或2次。

phone = re.search(r'(\d+-?){1,2}$', addr_str)

In case your string always contains Phone: with the phone number following it at the end, you do not need the regex. 如果您的字符串始终包含Phone: ,并且电话号码末尾跟着电话,则不需要正则表达式。 Also, note that 1-800-MALL is also a valid phone number. 另外,请注意1-800-MALL也是有效的电话号码。

I suggest this : 我建议这样

addr_str = "No 123 4th St, 5th Ave NYC\n\tPhone: 1-800-MALL"
idx = addr_str.find("Phone: ")
if idx > -1:
    print addr_str[idx+7:]
else:
    print addr_str

Or, in case regex is still preferable, another solution : 或者,如果仍推荐使用regex,则使用另一种解决方案

import re
addr_str = "No 123 4th St, 5th Ave NYC\n\tPhone: 1-800-MALL"
print re.search(r"Phone:\s*(.*)$", addr_str).group(1)

Assuming you want to allow only one hyphenated section then you can do this using an optional group 假设您只允许使用一个连字符部分,则可以使用可选组

((\d+-)?\d+)$

Demonstration: https://regex101.com/r/wV6zP7/1 示范: https : //regex101.com/r/wV6zP7/1

For example, this will match "0123-456789" but not "0123-456-789". 例如,这将匹配“ 0123-456789”,但不匹配“ 0123-456-789”。

如果您在电话号码之前始终留有空格,为什么不简单:

phone = addr_str[addr_str.rfind(' ') + 1:]

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

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