如何将筛选器列表拆分为单个筛选器元素? split2String结果为: 线程“主”中的异常java.util.regex.PatternSyntaxException:索引10附近的未封闭组 或(| AND(
public class TestIt {
public static void main(String[] args) {
// condition is Part of the Thunderbird Filter List
String condition1 = "OR (subject,contains,Xxxx Yyyy) OR (subject,contains,Uuuu Cccc) AND (from,contains,Wwwww Zzzzz) OR (subject,contains,Uuuu Cccc)";
String condition2 = "OR (subject,contains,Xxxx YyOR yy) OR (subject,contains,Uuuu CcAND cc) AND (from,contains,Wwwww Zzzzz) OR (subject,contains,Uuuu Cccc)";
split1String(condition1);
split1String(condition2);
split2String(condition1);
split2String(condition2);
/*
* Expected Result with condition2:
* (subject,contains,Xxxx YyOR yy)
* (subject,contains,Uuuu CcAND cc)
* (from,contains,Wwwww Zzzzz)
* (subject,contains,Uuuu Cccc)
*
*/
}
public static void split1String(String condition) {
// Syntax=OK Result wrong
String[] xsplitted = condition.split("OR |AND ");
for (int i = 0; i < xsplitted.length; i++) {
System.out.println(xsplitted[i]);
}
}
public static void split2String(String condition) {
// Syntax=NOT OK
String[] xsplitted = condition.split("OR (|AND (");
for (int i = 0; i < xsplitted.length; i++) {
System.out.println(xsplitted[i]);
}
}
答案 0 :(得分:0)
您可以使用以下正则表达式提取信息。因为您需要的全部包含在括号内。
Pattern pattern = Pattern.compile("\\((.*?)\\)", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(condition2);
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}
答案 1 :(得分:0)
"OR (|AND ("
此正则表达式的语法不正确。圆括号字符(
在正则表达式中具有特殊含义。它标志着capturing group的乞求。由于它是一个特殊字符,因此必须通过添加\\
来成为escaped:
"OR (subject,contains,Xxxx Yyyy) OR (subject,contains,Uuuu Cccc) AND (from,contains,Wwwww Zzzzz) OR (subject,contains,Uuuu Cccc)".split("OR \\(|AND \\(")
这给出了:
String[5] { "", "subject,contains,Xxxx Yyyy) ", "subject,contains,Uuuu Cccc) ", "from,contains,Wwwww Zzzzz) ", "subject,contains,Uuuu Cccc)" }
因此正确的正则表达式如下:"OR \\(|AND \\("