您可以使用下面的正则表达式来获取键值对。
([a-zA-Z0-9]+):"(.*?)(?<!\\)"
OR
([a-zA-Z0-9]+):"(.*?)"(?=,[a-zA-Z0-9]+:"|$)
DEMO
Java正则表达式是,
"([a-zA-Z0-9]+):\"(.*?)(?<!\\\\)\""
(?<!\\)"
负向后查找断言双引号前面不会有反斜杠字符。在java中,要匹配反斜杠字符,您需要将模式中的反斜杠转义三次,即\\\\
DEMO
String s = "joe:\"Look over there\\, it's a shark!\",sam:\"I like fish.\"";
Matcher m = Pattern.compile("([a-zA-Z0-9]+):\"(.*?)(?<!\\\\)\"").matcher(s);
while(m.find())
{
System.out.println(m.group(1) + " --> " + m.group(2));
}
}
Output:
joe --> Look over there\, it's a shark!
sam --> I like fish.
OR
String s = "joe:\"Look over there\\, i\\\"t's a shark!\",sam:\"I like fish.\"";
Matcher m = Pattern.compile("([a-zA-Z0-9]+):\"((?:\\\\\"|[^\"])*)\"").matcher(s);
while(m.find())
{
System.out.println(m.group(1) + " --> " + m.group(2));
}
}
Output:
joe --> Look over there\, i\"t's a shark!
sam --> I like fish.