-3

我有一个带有语法的编码字符串

"encodedProp:encodedValue OPERATOR encodedProp1:encodedValue1"

(OPERATOR 可能是AND, OR, NOT, 并且有 N 对prop:value)。

, "encodedProp", "encodedValue", "encodedProp1"..."encodedValue1"是编码字符串。

我想使用正则表达式来替换":"by " = "。此外,左侧的部分":"应替换为"\"" + left_part + "\"",右侧的部分应替换为"'" + right_part + "'"

对于上面的例子,替换后的字符串应该是:

"\"encodedProp\" = 'encodedValue' OPERATOR \"encodedProp1\" = 'encodedValue1'"

我必须用什么表达来做到这一点?

4

1 回答 1

0

好的,因为问题没有明确定义,所以我在这里冒险,但让我们试一试。

String resultString = subjectString.replaceAll(
    "(?x)(       # Match and capture in backreference number 1:\n" +
    " [^\\s:]+   #  one or more characters except spaces or colons\n" +
    ")           # End of capturing group 1\n" +
    ":           # Match a colon\n" +
    "(           # Match and capture in backreference number 2:\n" +
    " [^\\s:]+   #  one or more characters except spaces or colons\n" +
    ")           # End of capturing group 2", 
    "\"$1\" = '$2'");

这完全忽略了这OPERATOR部分 - 它只是查找包含一个冒号的字符序列,并将它们包裹在单引号/双引号中,一路替换冒号。

于 2012-05-15T13:28:47.740 回答