### 將字符串分片
`RegexObject` 的 split() 方法在 RE 匹配的地方將字符串分片,將返回列表。它同字符串的 split() 方法相似但提供更多的定界符;split()只支持空白符和固定字符串。就象你預料的那樣,也有一個模塊級的 re.split() 函數。
```
split(string [, maxsplit = 0])
```
通過正則表達式將字符串分片。如果捕獲括號在 RE 中使用,那么它們的內容也會作為結果列表的一部分返回。如果 maxsplit 非零,那么最多只能分出 maxsplit 個分片。
你可以通過設置 maxsplit 值來限制分片數。當 maxsplit 非零時,最多只能有 maxsplit 個分片,字符串的其余部分被做為列表的最后部分返回。在下面的例子中,定界符可以是非數字字母字符的任意序列。
```
#!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', '.', '']
```
模塊級函數 re.split() 將 RE 作為第一個參數,其他一樣。
```
#!python
>>> re.split('[\W]+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split('([\W]+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split('[\W]+', 'Words, words, words.', 1)
['Words', 'words, words.']
```