0

我有一个字符串,其中包含使用布尔运算符的条件,例如:

 1)   OR(key1:value1,key2:value2,key3:value3)

 2)   AND(key1:value1,key2:value2,key3:value3,key4:value4,key5:value5)

 3)   NOT(key1:value1,key2:value2)

 4) AND(key1:value1,key2:value2,key3:value3,OR(key4:value4,key5:value5),NOT(key6:value6,key7:value7))

现在我想将其转换为另一种格式,如下所示:

1)    key1='value1' OR key2='value2' OR key3='value3'

2)    key1='value1' AND key2='value2' AND key3='value3' AND key4='value4' AND key5='value5'

3)    NOT(key1='value1' OR key2='value2')

4)    ( key1='value1' AND key2='value2' AND key3='value3' ) AND ( key4='value4' OR key5='value5') AND ( NOT(key6='value6' OR key7='value7'))

我正在浏览这个链接:想要将一种形式的字符串转换成另一种形式?

但我不清楚如何使用正则表达式来实现这一点。请让我知道如何在 Java 中执行此操作

4

1 回答 1

1

使用String.replaceAll()方法。

public String replaceAll(String regex,
                         String replacement)

Replaces each substring of this string that matches the given regular 
expression with the given replacement. 

因此,例如,对于第一个字符串 -

"OR(key1:value1,key2:value2,key3:value3)"

你可以有

String string = "OR(key1:value1,key2:value2,key3:value3)";

string = string.replaceAll("OR","");
string = string.replaceAll("(","");
string = string.replaceAll(")","'");
string = string.replaceAll(":","='");
string = string.replaceAll(",","' OR ");
于 2013-04-26T17:01:03.600 回答