Korhan Ozturk
Korhan Ozturk

Reputation: 11308

Use of Scala's Parsers repsep to split the String

Been trying to parse the following string using Scala parser combinators

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

Want to get the following out of it:

Group1

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

Group2

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

Some other stuff here

I wrote the following using repsep but getting an error when I run it on the same input.

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

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

And the error is:

failure: string matching regex \z' expected but-' found

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

^

Please help me figure out what I'm doing wrong. Thanks

Upvotes: 2

Views: 112

Answers (1)

vks
vks

Reputation: 67968

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

Use this as the last --batch might be the end of string.So give it an alternative too.See demo.

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

Upvotes: 1

Related Questions