【发布时间】:2017-09-26 19:14:05
【问题描述】:
import re
string = "some text \n\n\nError on the field: more\n text and lines\n\n\nError on the field: some more\n lines \n\n\nError on the field: final lines"
pieces = re.split(r'(Error on the field:)', string, re.IGNORECASE)
pieces
['some text \n\n\n', 'Error on the field:', ' more\n text and lines\n\n\n', 'Error on the field:', ' some more\n lines \n\n\nError on the field: final lines']
pieces2 = re.split(r'(Error on the field:)', pieces[4], re.IGNORECASE)
pieces2
[' some more\n lines \n\n\n', 'Error on the field:', ' final lines']
为什么'Error on the field:'的第三个split在pieces的初始split中没有被拾取,而在pieces[4]的拆分时却被拾取?
【问题讨论】:
-
只要使用
re.split(r'(?i)(Error on the field:)', string)