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

3
  • "--batch_36522ad7-fc75-4b56-8c71-56071383e77b, misses a closing quote. Commented Mar 18, 2015 at 11:57
  • Updated the input string. The error still remains the same. @Avinash Raj Commented Mar 18, 2015 at 12:20
  • Have a look at a similar issue at stackoverflow.com/a/5460948/3832970. Perhaps, you need to add whitespace support to your code. Commented Mar 18, 2015 at 13:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.