2017-05-30 94 views
-3

我有一個任務來創建一個任務,該任務將獲取數據並將其放入日誌文件中。我有這個示例文本文件,它包含像這樣的塊/線。如何使用python替換文本文件中的碎塊

START OF SAMPLE 

lines of sample 
lines of sample 
lines of sample  

END OF SAMPLE 

START OF SAMPLE 

lines of sample 
lines of sample 
lines of sample 
linSTART OF SAMPLE 

lines of sample 
lines of sample 
lines of sample 

START OF SAMPLE 

lines of sample 
lines of sample 
lines of sample 

END OF SAMPLE 

正如你所看到的,還有就是樣品中破塊。我想找到與這樣的替換:

START OF SAMPLE 

Corrupted SAMPLE 

END OF SAMPLE 

即使有樣品沒有END該塊,我想擁有它。

回答

0

以下正則表達式將匹配所有有效的樣本。之後,您可以根據自己的需求做任何事情來處理這些數據。

START OF SAMPLE(?:(?!START OF SAMPLE).)+?END OF SAMPLE 

以下正則表達式需要DOTALL標誌。有關python中正則表達式的更多數據,請閱讀the documentation