#!python >>>p=re.compile(r'"W+') >>>p.split('This is a test, short and sweet, of split().') ['This','is','a','test','short','and','sweet','of','split',''] >>>p.split('This is a test, short and sweet, of split().',3) ['This','is','a','test, short and sweet, of split().']
有时,你不仅对定界符之间的文本感兴趣,也需要知道定界符是什么。如果捕获括号在 RE 中使用,那么它们的值也会当作列表的一部分返回。比较下面的调用:
#!python >>>p=re.compile(r'"W+') >>>p2=re.compile(r'("W+)') >>>p.split('This is a test.') ['This','is','a','test',''] >>>p2.split('This is a test.') ['This','','is','','a','','test','.','']