简体   繁体   中英

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 = 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.

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. Also, note that 1-800-MALL is also a valid phone number.

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 :

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

For example, this will match "0123-456789" but not "0123-456-789".

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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