1

私はここでコードを満たしました:

>>> 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

1inはどういうmatch.group(1)意味ですか?

4

2 に答える 2

7

1inは、最初のmatch.group(1)括弧で囲まれたサブグループを表します。あなたの場合、最初のもの。

これは、の公式ドキュメントで詳しく説明さre.MatchObject.group()れており、例を使用して最もよく説明されています。

>>> 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')
于 2013-02-16T11:34:17.143 に答える
3

1は、表現内のグループの番号(括弧のペアで示されます)です。式にはグループが1つしかありませんが、:との一致"123 456"を想像してみてください。は、であり、常に完全に一致するため、。r"(\d+) (\d+)"group(1)"123"group(2)"456"group(0)"123 456"

于 2013-02-16T11:32:49.323 に答える