I am a beginner and I was planning to create a regex to find phone numbers and, if available, the phone extension in a sample text. I am using the option “findall”. I had spent some good time trying and changing the code and this what I thought the most effective. I am having a hard time to understand why the terminal is giving these results.
I am not sure what have I missed here. If I remove the extension section it works fine, but as soon as I add it, it can find anything.
import re
text=" This is my number (801)-804-2121 ext 458, my NEW PHONE IS 375-704-5121,work phone is 805.544.2335 and my wifes is 458 8458"
phoneNumberReg = re.compile(r''' \(?\d{3}\)?
\D{0,3} #separating
\d{3}
\D{0,3} #separating
\d{4}
\s*(ext|x|ext.)?
\s*(\d{2,5})?
''', re.VERBOSE)
resultsPhone = phoneNumberReg.findall(text)
print(resultsPhone)
Terminal:
['', '', '']
[('ext', '458'), ('', ''), ('', '')]
Also, If I make the area code as optional to find number 000-0000, it will only find the first part of the number.
(\(?\d{3}\)?)?
Terminal:
['', '', '']
['(801)', '375', '805', '']