Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-Regex, what's going on here?

Tags:

python

regex

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'
like image 588
user33061 Avatar asked Jan 11 '09 18:01

user33061


2 Answers

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.

like image 69
SchaeferFFM Avatar answered Sep 18 '22 14:09

SchaeferFFM


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.

like image 21
tristan Avatar answered Sep 20 '22 14:09

tristan