使用regex在json中使用scala删除多个双引号

3mpgtkmj  于 2021-05-29  发布在  Spark
关注(0)|答案(1)|浏览(487)

我有这个json字符串
{“last\u nm”:“难以辨认”,“ful\u nm”:“}
下面是替换代码,我试图在scala中删除上面json中多余的双引号,但它不起作用。

replace(":\"\"[a-z]+\"\"",":\"[a-z]+\"")
ufj5ltwl

ufj5ltwl1#

您可以使用正则表达式捕获组,方法是将正则表达式括在省略号中- (some_regex) ,然后用 $x (x=组号)。例如:

scala> val str = "{\"LAST_NM\":\"\"ILLEGIBLE\"\",\"FULL_NM\":\"\",\"FIRST_NAME\":\"\"NOT_SUPPLIED\"\"}"
str: String = {"LAST_NM":""ILLEGIBLE"","FULL_NM":"","FIRST_NAME":""NOT_SUPPLIED""}

scala> str.replaceAll("\"(\"[^\\,]+?\")\"","$1")
res5: String = {"LAST_NM":"ILLEGIBLE","FULL_NM":"","FIRST_NAME":"NOT_SUPPLIED"}

相关问题