简体   繁体   中英

Split String with multiple String Delimiter

How can I split the Filterlist in single Filter Elements? split2String results in an: Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 10 OR (|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]);
        }
    }

you can use the following regex to extract information. As all you need is contained within parenthesis.

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));
  }
}

"OR (|AND (" this regex has an incorrect syntax. Round bracket character ( has a special meaning in regex. It marks the begging of capturing group . Since it is a special character, it must be escaped by adding \\ :

"OR (subject,contains,Xxxx Yyyy) OR (subject,contains,Uuuu Cccc) AND (from,contains,Wwwww Zzzzz) OR (subject,contains,Uuuu Cccc)".split("OR \\(|AND \\(")

this gives:

 String[5] { "", "subject,contains,Xxxx Yyyy) ", "subject,contains,Uuuu Cccc) ", "from,contains,Wwwww Zzzzz) ", "subject,contains,Uuuu Cccc)" }

So the correct regex looks like: "OR \\(|AND \\("

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM