Extract phone number using regex with different formats python

Viewed 414

Hi I have a string of texts which include a phone number in many different formats, I need to be able to extract just the phone number.

Eg: "Hi my name is marc and my phone number is 03-123456 and i would like 2 bottles of water 0.5L"

possible phone formats:

  • All phone numbers begin with area code either 03 or 70 or 71 or 76 (no other option)
  • all phone numbers are 6 digits after the area code
  • Different msgs have different formats such as 03-123456 or 03123456 or 03 123 456 or 03 123456 or 03/123456 (you get the idea)

I am able to find the index using find function in python by looking for (03 or 70 or 76 or 71) but I am not able to find the index of the last number.

number_start = message.find('03' or '70' or '76' or '71')

Any ideas?

1 Answers

You could use

\b(?:03|7[016])[- /]?\d{3} ?\d{3}\b

Explanation

  • \b A word boundary
  • (?:03|7[016]) Match one of 03 70 71 76
  • [- /]? Optionally match - a space or /
  • \d{3} ?\d{3} Match 6 digits with an optional space after the 3rd digits
  • \b A word boundary

Regex demo | Python demo

For example

import re

regex = r"\b(?:03|7[016])[- /]?\d{3} ?\d{3}\b"
test_str = "Hi my name is marc and my phone number is 03-123456 and i would like 2 bottles of water 0.5L"
matches = re.search(regex, test_str)

if matches:
    print(matches.group())

Output

03-123456
Related