2015-03-19 28 views
2

一直試圖使用Scala的解析器組合來分析以下字符串使用Scala的解析器repsep分割字符串

--batch_36522ad7-fc75-4b56-8c71-56071383e77b 
Content-Type: application/http 
Some stuff in here  

--batch_36522ad7-fc75-4b56-8c71-56071383e77b 
Content-Type: multipart/mixed;boundary=changeset_77162fcd-b8da-41ac-a9f8-9357efbbd 

Some other stuff here 

想獲得以下出來的:

組別1

--batch_36522ad7-fc75-4b56-8c71-56071383e77b 
Content-Type: application/http 
Some stuff in here  

組2

--batch_36522ad7-fc75-4b56-8c71-56071383e77b 
Content-Type: multipart/mixed;boundary=changeset_77162fcd-b8da-41ac-a9f8-9357efbbd 

Some other stuff here 

我使用repsep編寫了以下內容,但在同一輸入上運行時出現錯誤。

def getListOfRequests: Parser[List[String]] = repsep(getBatchModules, newLineSeparator) 

def getBatchModules: Parser[String] = """(?s)--batch_.+?(?=--batch.*)""".r 

和錯誤是:

失敗:字符串匹配的正則表達式\z' expected but - 」發現

--batch_36522ad7-fc75-4b56-8c71-56071383e77b

^

請幫我弄清楚我在做什麼w榮。謝謝

回答

1
--batch_.+?(?=--batch|$) 

使用此作爲最後--batch可能是字符串的結尾。請給它一個替代太。請參閱演示。

https://regex101.com/r/pT4tM5/18

+0

原來,這是我使用的重複導致錯誤的分隔符。反正你的答案是正確的。謝謝! – 2015-03-19 10:52:36