αƞjiβ
αƞjiβ

Reputation: 3246

Matching regular expression in Scala

I am trying to test string like {"count":0} in Scala using matches. Since Integer part can be different I am trying to do something like this:

assert(response.matches(s"^\\{\"count\":${notificationCount}\\}$"), s"Actual response: $response")

But I am getting wrong string literal in second $ sign which indicate end of string in regular expression.

Any suggestion?

Upvotes: 0

Views: 398

Answers (1)

Ben Reich
Ben Reich

Reputation: 16324

When using string interpolation in Scala, you can escape $ by using a double $$:

val foo = 5
s"$foo${foo + 1}$$" //56$

You might also consider using a triple-quoted raw string to help with the escaped brace and quote characters:

s"""{"count":${foo}}$$""" //{"count":5}$

Upvotes: 3

Related Questions