Search code examples
javaregexbrackets

Java Regex - split comma separated list, but exclude commas within parentheses


I'm trying to write regex that will split a Java string like this:

300x250,468x60,300x400v(480x320,768x1024,100x100),400x300v,640x480v(200x200,728x90)

in to something like this:

300x250 
468x60
300x400v(480x320,768x1024,100x100)
400x300v
640x480v(200x200,728x90)

I've been trying \,(\()? but this ends up selecting the commas in the parentheses as well.


Solution

  • If you have to use regex you can split on ,(?![^(]*\\))

    If not then one simple iteration over characters can do the trick

    String data="300x250,468x60,300x400v(480x320,768x1024,100x100),400x300v,640x480v(200x200,728x90)";
    
    List<String> tokens=new ArrayList<>();
    StringBuilder buffer=new StringBuilder();
    
    int parenthesesCounter=0;
    
    for (char c : data.toCharArray()){
        if (c=='(') parenthesesCounter++;
        if (c==')') parenthesesCounter--;
        if (c==',' && parenthesesCounter==0){
            //lets add token inside buffer to our tokens
            tokens.add(buffer.toString());
            //now we need to clear buffer  
            buffer.delete(0, buffer.length());
        }
        else 
            buffer.append(c);
    }
    //lets not forget about part after last comma
    tokens.add(buffer.toString());
    
    String[] splitedArray=tokens.toArray(new String[tokens.size()]);
    
    //lets test what is inside our array
    for (String s : splitedArray)
        System.out.println(s);
    

    Output

    300x250
    468x60
    300x400v(480x320,768x1024,100x100)
    400x300v
    640x480v(200x200,728x90)