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