gpt4 book ai didi

java - 将字符串查询从一种形式转换为另一种形式

转载 作者:行者123 更新时间:2023-12-01 14:38:29 29 4
gpt4 key购买 nike

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

 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'))

我正在浏览此链接:Want to convert a string in one form into another form?

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

最佳答案

使用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 ");

关于java - 将字符串查询从一种形式转换为另一种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16241606/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com