I’ve got a book on python recently and it’s got a chapter on Regex, there’s a section of code which I can’t really understand. Can someone explain exactly what’s going on here (this section is on Regex groups)?
>>> my_regex = r'(?P<zip>Zip:\s*\d\d\d\d\d)\s*(State:\s*\w\w)' >>> addrs = 'Zip: 10010 State: NY' >>> y = re.search(my_regex, addrs) >>> y.groupdict('zip') {'zip': 'Zip: 10010'} >>> y.group(2) 'State: NY'
regex definition:
Creates a named group ‘zip’
Match ‘Zip:’ and zero or more whitespace characters
Match a digit
Match a word character [A-Za-z0-9_]
The groupdict method returns a dictionary with named groups as keys and their matches as values. In this case, the match for the ‘zip’ group gets returned
Return the match for the second group, which is a unnamed group ‘(…)’
Hope that helps.