Krithika Raghavendran
Krithika Raghavendran

Reputation: 457

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.

Upvotes: 1

Views: 3482

Answers (6)

marcusholmgren
marcusholmgren

Reputation: 134

You could have your digit pattern to include optional minus sign and expect the group to be repeated 1 or 2 times.

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

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627400

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)

Upvotes: 1

Holt
Holt

Reputation: 37661

If you always have a space before the phone number, why not simply:

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

Upvotes: 0

vks
vks

Reputation: 67988

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

You can simply modify your regex to this.If there is always a possiblity of only 1 - use

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

Upvotes: 1

Daniel Renshaw
Daniel Renshaw

Reputation: 34187

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

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174836

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)

Upvotes: 2

Related Questions