Definition: re.split(pattern, string, maxsplit=0, flags=0)
如下将字符串按空格分开,maxsplit是分离的次数,maxsplit=1分离一次,默认为0,不限制次数
In [67]: text Out[67]: 'HTTP/1.1 301 Moved Permanently' In [68]: re.split(r"\s+",text) Out[68]: ['HTTP/1.1', '301', 'Moved', 'Permanently'] In [69]: re.split(r"\s+",text,0) Out[69]: ['HTTP/1.1', '301', 'Moved', 'Permanently'] In [70]: re.split(r"\s+",text,1) Out[70]: ['HTTP/1.1', '301 Moved Permanently'] In [71]: re.split(r"\s+",text,2) Out[71]: ['HTTP/1.1', '301', 'Moved Permanently'] In [72]: re.split(r"\s+",text,3) Out[72]: ['HTTP/1.1', '301', 'Moved', 'Permanently'] In [73]: re.split(r"\s+",text,-1) Out[73]: ['HTTP/1.1 301 Moved Permanently']
如果用括号将正则表达式括起来,那么匹配的字符串也会被列入到list中返回;如果用非捕获分组与非分组结果一致。
In [75]: re.split('\W+', 'Words, words, words.') Out[75]: ['Words', 'words', 'words', ''] In [76]: re.split('(\W+)', 'Words, words, words.') Out[76]: ['Words', ', ', 'words', ', ', 'words', '.', ''] In [77]: re.split('\W+', 'Words, words, words.', 1) Out[77]: ['Words', 'words, words.'] In [12]: re.split('((?:)\W+)', 'Words, words, words.') Out[12]: ['Words', 'words', 'words', '']
如果在字符串的开始或结尾就匹配,返回的list将会以空串开始或结尾。
In [78]: re.split('(\W+)', '...words, words...') Out[78]: ['', '...', 'words', ', ', 'words', '...', '']
In [83]: re.split("c","python is better") Out[83]: ['python is better']