Java 用于匹配奇数大括号后字符的正则表达式
我需要将Java 用于匹配奇数大括号后字符的正则表达式,java,regex,Java,Regex,我需要将a\nb替换为a\\nb,以使下面的字符串成为有效的json。 众所周知,在所需的a\n 什么是正则表达式 { 'key1': 'a b', 'key2':'value2', 'key3':'value3' } 下面是一个使用replaceAll()的解决方案 编辑:将replaceAll(“\n”,“\\n”)更改为replaceAll(“a\nb”,“a\\nb”)以下是使用replaceAll()的解决方案 编辑:将replaceAll(“\n”,“\\n
a\nb
替换为a\\nb
,以使下面的字符串成为有效的json。众所周知,在所需的
a\n
什么是正则表达式
{
'key1': 'a
b',
'key2':'value2',
'key3':'value3'
}
下面是一个使用
replaceAll()的解决方案
编辑:将
replaceAll(“\n”,“\\n”)
更改为replaceAll(“a\nb”,“a\\nb”)
以下是使用replaceAll()的解决方案
编辑:将replaceAll(“\n”,“\\n”)
更改为replaceAll(“a\nb”,“a\\nb”)
谢谢您的回答!我已经试过了,它用\\n
替换了所有其他的换行符,这破坏了jsonString。replaceAll()是您的答案!我已经试过了,它用\\n
替换了所有其他的换行符,这会破坏jsonString。replaceAll()是regex
public class Program
{
public static void main(String[] args) {
String str = "{
'key1': 'a
b',
'key2':'value2',
'key3':'value3'
}";
str = str.replaceAll("a\nb", "a\\nb");
System.out.println(str);
}
}