I'm trying to create a function to capture phone numbers written in a canonical form (XXX)XXX-XXX or XXX-XXX-XXXX with additional conditions. This is my approach
def parse_phone2(s):
phone_number = re.compile(r'''^\s*\(? # Begining of string, Ignore leading spaces
([0-9]{3}) # Area code
\)?\s*|-? # Match 0 or 1 ')' followed by 0 or more spaces or match a single hyphen
([0-9]{3}) # Three digit
-? # hyphen
([0-9]{4}) # four digits
\s*$ # End of string. ignore trailing spaces''', re.VERBOSE)
try:
return (phone_number.match(s).groups())
except AttributeError as e:
raise ValueError
I was failing this test case ' (404) 555-1212 '
but another question of SO suggest me to replace \)?\s*|-?
by (?:\)?\s*|-?)
and it works. The problem is that I don't understand the difference between both nor the purpose of (?:...)
further than create non-capturing groups. The docs aren't clear enough for me as well.