我有一个像这样的字符串
((one = WORD) AND (two != 2) AND (three > 3) AND (four >= 4) AND (five < 5) AND (six <= 6) AND (seven IN (seven1, seven2)) AND (eight LIKE 'like'))
我需要一个匹配所有组的模式(Java),注意IN运算符的内部组。我需要这样的结果
one = WORD
two != 2
three > 3
four >= 4
five < 5
six <= 6
seven IN (seven1, seven2)
eight LIKE 'like'
从(
到下一个)
匹配的单个模式不正常,因为对于IN运算符,有一个内部组
谢谢
答案 0 :(得分:0)
使用this page中的模式,我写了这个示例代码
String string = "((one = WORD) AND (two != 2) AND (three > 3) AND (four >= 4) AND (five < 5) AND (six <= 6) AND (seven IN (seven1, seven2)) AND (eight LIKE '1,2,3'))";
long start_time = System.currentTimeMillis();
Pattern pattern = Pattern.compile("\\(([^()]*|\\([^()]*\\))*\\)");
Matcher matcher = pattern.matcher(string);
while (matcher.find())
System.out.println(matcher.group());
long stop_time = System.currentTimeMillis();
System.out.println("Time " + (stop_time - start_time) + "ms");
输出很完美,但性能令人尴尬,7-8秒
(one = WORD)
(two != 2)
(three > 3)
(four >= 4)
(five < 5)
(six <= 6)
(seven IN (seven1, seven2))
(eight LIKE '1,2,3')
Time 8017ms
编辑:现在已经解决了这个问题
string = string.substring(2, string.length() - 2);
String[] split = string.split("\\) AND \\(");
for (String s : split)
System.out.println(s);