re.split()切割功能非常强大
>>> import re
>>> line = 'aaa bbb ccc;ddd eee,fff'
>>> line
'aaa bbb ccc;ddd eee,fff'
单字符切割
>>> re.split(r';',line)
['aaa bbb ccc', 'ddd\teee,fff']
两个字符以上切割需要放在 [ ] 中
>>> re.split(r'[;,]',line)
['aaa bbb ccc', 'ddd\teee', 'fff']
所有空白字符切割
>>> re.split(r'[;,\s]',line)
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
使用括号捕获分组,默认保留分割符
>>> re.split(r'([;])',line)
['aaa bbb ccc', ';', 'ddd\teee,fff']
不想保留分隔符,以(?:…)的形式指定
>>> re.split(r'(?:[;])',line)
['aaa bbb ccc', 'ddd\teee,fff']