val REGEX_OPEN_CURLY_BRACE = """\{""".r
val REGEX_CLOSED_CURLY_BRACE = """\}""".r
val REGEX_INLINE_DOUBLE_QUOTES = """\\\"""".r
val REGEX_NEW_LINE = """\\\n""".r
// Replacing { with '{' and } with '}'
str = REGEX_OPEN_CURLY_BRACE.replaceAllIn(str, """'{'""")
str = REGEX_CLOSED_CURLY_BRACE.replaceAllIn(str, """'}'""")
// Escape \" with '\"' and \n with '\n'
str = REGEX_INLINE_DOUBLE_QUOTES.replaceAllIn(str, """'\"'""")
str = REGEX_NEW_LINE.replaceAllIn(str, """'\n'""")
是否有更簡單的方法來組合和替換所有這些{,},\",\n
?scala正則表達式組匹配和替換
我仍然不確定你想用引號做什麼,但我認爲這就是你現在要做的...... – DaoWen
並且用較少的反斜槓:「」「{」\ n}「」「」。 replaceAll(「」「([」{} \\ n])「」「,」'$ 1'「) – yakshaver
@yakshaver - 您的示例將分別替換'n'和'\',例如'「no」'=>'「'n'o」'。至於引號前面的反斜線,這就是爲什麼我說我不確定他想用引號做什麼。我想他可能實際上是在尋找''''而不是''''自己。 – DaoWen