假定
除行终止符之外的任何字符都指定紧随其后的字符。您可以使用以下正则表达式来匹配键值对的所有实例:
"([a-zA-Z0-9]+):"((?:[^\\"]|\\.)*+)""
如果要允许自由间距
\s*,请在之前和之后添加
:。
这是正则表达式引擎看到的内容:
([a-zA-Z0-9]+):"((?:[^\"]|\.)*+)"
量词
*由所有格
*+,由于2个分支
[^\"]并
\.是互斥(没有字符串可由两个在同一时间相匹配)。它还避免
StackOverflowError了在Oracle的实现
Pattern类。
在Matcher循环中使用上述正则表达式:
Pattern keyValuePattern = Pattern.compile("([a-zA-Z0-9]+):"((?:[^\\"]|\\.)*+)"");Matcher matcher = keyValuePattern.matcher(inputString);while (matcher.find()) { String key = matcher.group(1); // Process the escape sequences in the value string String value = matcher.group(2).replaceAll("\\(.)", "$1"); // ...}在一般的情况下,根据不同的转义序列的复杂性(例如
n,
uhhhh,
xhh,


