我知道原始字符串可以声明为:
val foo: String = """foo"""
或者
val foo: String = raw"foo"
但是,如果我有一个字符串类型 val,我怎样才能将它转换为 raw?例如:
// val toBeMatched = "line1: foobarfoo\nline2: lol"
def regexFoo(toBeMatched: String) = {
val pattern = "^.*foo[\\w+]foo.*$".r
val pattern(res) = toBeMatched /* <-- this line induces an exception
since Scala translates '\n' in string 'toBeMatched'. I want to convert
toBeMatched to raw string before pattern matching */
}