1

I'm new to Scala and trying to parse a very simple String and get every character until encountering "--batch" with the following String parser:

def getEntireMetaData : Parser[EntireMetaData] = """(?s).+?(?=--batch)""".r ^^ { EntireMetaData}

And I call it as the following:

val batchRequest: String = "POST /service/$batch HTTP/1.1 \nHost: host \n +
"Content-Type: multipart/mixed;boundary=batch_36522ad7-fc75-4b56-8c71-56071383e77b\n \n" +
"--batch_36522ad7-fc75-4b56-8c71-56071383e77b "

implicit val p = parser.getEntireMetaData
parser.parseAll(p, batchRequest) match {
  case result: parser.Success[_] => println(result.get) 
  case result: parser.NoSuccess => fail(result.toString) 
}

which gives me the error

[7.1] failure: string matching regex `\z' expected but `-' found

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

^

The following is what I want my parser to match:

"POST /service/$batch HTTP/1.1 \nHost: host \n +
"Content-Type: multipart/mixed;boundary=batch_36522ad7-fc75-4b56-8c71-56071383e77b\n \n"

Please help me sort this out.

Thanks in advance

Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49

1 Answers1

1

Ok, you have a few different issues:

  1. You are not actually catching your match
  2. You are missing a " after \nHost: host \n

Overall, the following expression does what you want: (?s)(.+?)(?=--batch)

On the other hand, you hardly need a regex for this:

batchRequest.substring(0, batchRequest.indexOf("--batch"))

gets you:

POST /service/$batch HTTP/1.1 Host: host Content-Type: multipart/mixed;boundary=batch_36522ad7-fc75-4b56-8c71-56071383e77b

You can also check if indexOf returns -1 before of course.

Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48
  • 1_ that is the multiline enable option (?s) not a character match 2_ (?=--batch) supposed do that? 3_ using multiline option 4_ you're right. How do I use the output of `batchRequest.substring(0, batchRequest.indexOf("--batch"))` in a parser? – Korhan Ozturk Mar 18 '15 at 16:04
  • Interesting. I had to look that up, I always passed flags directly and never used embedded flag expression before. Ok, so 1 and 3 are actually the same thing, solved. For 2, just put a normal parenthesis around the text you want, this will catch that group: `(.+?)` followed by `(?=--batch)` which is a non-catching group. As I mentioned above. I'll edit the answer to make it clearer... – Daniel Langdon Mar 18 '15 at 18:24
  • As for the parser. You can always create a custom one around this code I guess. But my point is that you probably don't need that extra complexity if you just need to extract that piece of a string. – Daniel Langdon Mar 18 '15 at 18:29