简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM