IDLE 1.1.4
>>> import re
>>> some_text = 'alpha, beta,,,,gamma delta'
>>> re.split('[, ]+', some_text)
['alpha', 'beta', 'gamma', 'delta']
# when the pattern doesn't contain parentheses, the returned values
# only include matched substrings but separators.
>>> re.split('([, ]+)', some_text)
['alpha', ', ', 'beta', ',,,,', 'gamma', ' ', 'delta']
# returned values include separators and I can guess how it works.
>>> re.split('([, ])+', some_text)
['alpha', ' ', 'beta', ',', 'gamma', ' ', 'delta']
# Now I cannot even guess what is going on here.
問題>是什麼'([, ]+)'
和'([, ])+'
之間的差異? 它如何影響返回值?匹配圖案差`([,] +)`和`([,])+`
您可能想嘗試與're.search()'匹配,看看匹配是什麼;分組的行爲在這裏引入了額外的複雜性。 – tripleee 2012-02-21 05:52:20