Separate a complex chain with Regex in JAVA

advertisements

I want to Split following strings to an array with Regex in JAVA but I don't know how to do.

string1="advmod(likes-4, also-3)"  ==>    advmod, likes, also
string2="nsubj(likes-4, dog24-2)"  ==>    bsubj, likes, dog24
string3="num(dog24-3, 8-2)"        ==>    num, dog24, 8

Please help me to do this work? how split the string like "num(dog24-3, 8-2)" in three tokens num, dog24 and 8 and then putting they to an string array.

Thanks a lot.


This is generic:

String string[] = {"advmod(likes-4, also-3)",// ==> advmod , likes , also
        "nsubj(likes-4, dog24-2)",// ==> bsubj , likes , dog24
        "num(dog24-3, 8-2)"};//==> num ,dog24 , 8
Pattern p = Pattern.compile("(\\w+)\\(([^-]+).*, ([^-]+)");
for (int i = 0; i < string.length; i++) {
    Matcher m = p.matcher(string[i]);
    while(m.find()) {
        System.out.print(i+": ");
        for(int j=1; j<= m.groupCount(); j++){
            System.out.print(m.group(j));
            if(j!=m.groupCount()) {
                System.out.print(", ");
            }
        }
        System.out.println("");
    }
}

Hope this helps, it works for me.

This is the output:

0: advmod, likes, also
1: nsubj, likes, dog24
2: num, dog24, 8