2017-03-23 58 views
1

我試圖編寫一些正則表達式,在比賽前返回1個字符。例如正則表達式 - 比賽前返回1個字符

:record1: Has text here with "\n" as the end of line \n 
however, there can be multiple line \n 
and I dont know how many before I get to the next record \n 
:record2: has some other date in it \n 

我現在的正則表達式,試圖找到RECORD1長相結束這樣

(\n)(?=:record2:) 

我還想捕獲上次\ n,但這樣我就可以得到它的位置。關於如何在字符串匹配之前立即捕獲字符的任何想法?

+0

'*(\ n):RECORD2:'會捕獲上次'\ N'字符串之前。但是,「位置」是什麼意思,以及您使用的是什麼工具/語言? –

+0

你爲什麼不用'\ n:record:'來分割你的字符串?(並刪除數字)? – antoni

+0

我使用專有工具將文本文件轉換爲xml。該工具允許您使用正則表達式定義開始和結束標籤。我想定義一個結束標記,以便在\ n之前標識\ n:record2: – TheUltimateHack

回答

0

因此,如果您不想僅按\n:record:(並刪除數字)將字符串拆分爲更簡單且更快速的字符,那麼您要查找的是名爲lookahead\\n\n(?=:record\d+:)

您可以查看這裏的演示:https://regex101.com/r/im02XA/1

+0

我會檢查並更新。謝謝 – TheUltimateHack