2017-09-26 64 views
2
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:'第三裂在pieces初始分裂沒有回升,但回升的時候你拆pieces[4]蟒蛇re.split不工作的所有領域

+0

只要使用're.split(r'(?i)(錯誤在字段:)',字符串)' – kaza

回答

5

re.split位置參數是:

  • 正則表達式
  • maxsplit(默認值:無限制)
  • 標誌(初始值:無標誌)

    split(pattern, string, maxsplit=0, flags=0)

您正在通過re.IGNORECASE(該標誌的值爲2)爲maxsplit參數(如postional),它解釋了奇怪的影響。它在某種程度上起作用,然後在2次分裂後按照指示停止分裂。

只要做flags=re.IGNORECASE(關鍵字,而不是位置),而不是它的工作原理。

re.compile可以傳遞標誌的位置安全:compile(pattern, flags=0),並且對於re.matchre.search是真的爲好,但不適合re.split & re.sub,所以它是一個簡單的陷阱掉進去。如有疑問,請始終爲可選參數使用傳遞關鍵字。

2

您需要使用re.split時註明要使用flags=明確使用標誌:

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, flags=re.I) 

print(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\n', 'Error on the field:', ' final lines'] 

注:re.Ire.IGNORECASE相同