Reputation: 67
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:
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?
Upvotes: 2
Views: 1115
Reputation: 163457
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 boundaryFor 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
Upvotes: 4