What does the 1 in match.group(1) mean?
Question:
I met the codes here:
>>> import urllib, re
>>> prefix = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
>>> findnothing = re.compile(r"nothing is (d+)").search
>>> nothing = '12345'
>>> while True:
... text = urllib.urlopen(prefix + nothing).read()
... print text
... match = findnothing(text)
... if match:
... nothing = match.group(1)
... print " going to", nothing
... else:
... break
What does the 1
in match.group(1)
mean?
Answers:
The 1 is the number of the group — denoted by the parentheses pair — in the the expresssion. Your expression has only one group, but imagine matching "123 456"
with r"(d+) (d+)"
: group(1)
would be "123"
, group(2)
would be "456"
and group(0)
is always the whole match, hence "123 456"
.
The 1
in match.group(1)
represents the first parenthesised subgroup. In your case the first of one.
This is well described in the official documentation for re.MatchObject.group()
and best illustrated with an example:
>>> m = re.match(r"(w+) (w+)", "Isaac Newton, physicist")
>>> m.group(0) # The entire match
'Isaac Newton'
>>> m.group(1) # The first parenthesized subgroup.
'Isaac'
>>> m.group(2) # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2) # Multiple arguments give us a tuple.
('Isaac', 'Newton')
I met the codes here:
>>> import urllib, re
>>> prefix = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
>>> findnothing = re.compile(r"nothing is (d+)").search
>>> nothing = '12345'
>>> while True:
... text = urllib.urlopen(prefix + nothing).read()
... print text
... match = findnothing(text)
... if match:
... nothing = match.group(1)
... print " going to", nothing
... else:
... break
What does the 1
in match.group(1)
mean?
The 1 is the number of the group — denoted by the parentheses pair — in the the expresssion. Your expression has only one group, but imagine matching "123 456"
with r"(d+) (d+)"
: group(1)
would be "123"
, group(2)
would be "456"
and group(0)
is always the whole match, hence "123 456"
.
The 1
in match.group(1)
represents the first parenthesised subgroup. In your case the first of one.
This is well described in the official documentation for re.MatchObject.group()
and best illustrated with an example:
>>> m = re.match(r"(w+) (w+)", "Isaac Newton, physicist")
>>> m.group(0) # The entire match
'Isaac Newton'
>>> m.group(1) # The first parenthesized subgroup.
'Isaac'
>>> m.group(2) # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2) # Multiple arguments give us a tuple.
('Isaac', 'Newton')