I'm trying to learn Python from ''Automate the Boring Stuff with Python'' and I came across a program which I don't clearly understand.
phone_regex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
(\d{3}) # first 3 digits
(\s|-|\.) # separator
(\d{4}) # last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
)''', re.VERBOSE)
matches = []
for groups in phone_regex.findall(text):
print('here')
phone_number = '-'.join([groups[1], groups[3], groups[5]])
if groups[8] != '':
phone_number += ' x' + groups[8]
matches.append(phone_number)
This is obviously not the whole thing, but I don't understand how IS there a groups[8] when there are only 6 groups in the regex. Additionally, while I do know the first group, group[0], is supposedly the first one, I don't really understand how...Does it just work like that, when there's one big tuple containing multiple tuples, the big one is considered the first one when indexed ?
This is obviously not the whole thing, but I don't understand how IS there a groups[8] when there are only 6 groups in the regex. Additionally, while I do know the first group, group[0], is supposedly the first one, I don't really understand how...Does it just work like that, when there's one big tuple containing multiple tuples, the big one is considered the first one when indexed ?
Also, how do the for loops here work exactly ? What are they looping ? I thought adding something like groups=groups[0:] was necessary for iterations to actually be different from one another in cases like this...
Thanks in advance.