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:
(?P<zip>...)
Creates a named group "zip"
Zip:\s*
Match "Zip:" and zero or more whitespace characters
\d
Match a digit
\w
Match a word character [A-Za-z0-9_]
y.groupdict('zip')
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
y.group(2)
Return the match for the second group, which is a unnamed group "(...)"
Hope that helps.
The search method will return an object containing the results of your regex pattern.
groupdict returns a dictionnary of groups where the keys are the name of the groups defined by (?P...). Here name is a name for the group.
group returns a list of groups that are matched. "State: NY" is your third group. The first is the entire string and the second is "Zip: 10010".
This was a relatively simple question by the way. I simply looked up the method documentation on google and found this page. Google is your friend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With