使用Scala的Parsers repsep来拆分String

时间:2015-03-19 10:29:45

标签: regex scala

尝试使用Scala解析器组合器解析以下字符串

--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

     

^

请帮我弄清楚我做错了什么。感谢

1 个答案:

答案 0 :(得分:1)

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

使用此作为最后--batch可能是字符串的结尾。所以也给它一个替代。参见演示。

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