2

これが私たちのテキストだとしましょう:

text = 'After 1992 , the winter and summer Olympics will be held two years apart , with the revised schedule beginning with the winter games in 1994 and the summer games in 1996 . ) Now , Mr. Pilson -- a former college basketball player who says a good negotiator needs `` a level of focus and intellectual attention  similar to a good athlete-s is facing the consequences of his own aggressiveness . Next month , talks will begin on two coveted CBS contracts'
print re.search(r'(\w+ |\W+ ){0,4}1992( \W+| \w+){4}', text).group(0)

出力: 1992年以降、冬と

しかし、これは私に与えます:

print re.search(r'(\w+ |\W+ ){0,4}1992( \W+| \w+){0,4}', text).group(0)

出力: 1992年以降、

なぜ2番目の正規表現が貪欲ではないので、私には奇妙に思えますか?

これは他のものより少し奇妙です:

print re.search(r'(\w+ |\W+ ){0,4}summer( \W+| \w+){0,4}', text).group(0)

アウトプット、冬季・夏季オリンピックが開催されます

質問

1-最初のものと2番目のものの違いは何ですか。私にとっては、唯一の違いは同じテキストを提供する必要があり、長い文字列を提供{0,4}する場合は、正規表現が貪欲であるため同じ文字列を提供する必要があります。{4}{0,4}

2- 3番目の例はとの両方で同じように機能するため、問題は句読点に関連している可能性が{0,4}あり{4}ます。

私は混乱しています。

4

1 に答える 1

4
于 2012-11-12T19:16:49.710 に答える