Java Regex - comma separated list, but exclude commas in parentheses

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

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

      

for something like this:

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

      

I'm trying \,(\()?

, but it also results in the selection of commas in parentheses.

Any help is appreciated!

+3


source to share


2 answers


If you need to use a regex you can split it by ,(?![^(]*\\))

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)

      

+5


source


akburg, resurrecting this question for completion because he had another simple solution that was not mentioned. This situation is similar to match (or replace) a pattern, except for situations s1, s2, s3, etc. ...

Here's our simple regex:

\([^)]*\)|(,)

      

The left side of the alternation corresponds to the full tag (parentheses)

. We will ignore these matches. The right side matches and fixes the commas for group 1, and we know they are right commas because they didn't match the expression on the left.



This program shows you how to use a regular expression (see the results at the bottom of the online demo ):

import java.util.*;
import java.io.*;
import java.util.regex.*;
import java.util.List;

class Program {
public static void main (String[] args) throws java.lang.Exception  {

String subject = "300x250,468x60,300x400v(480x320,768x1024,100x100),400x300v,640x480v(200x200,728x90)";
Pattern regex = Pattern.compile("\\([^)]*\\)|(,)");
Matcher m = regex.matcher(subject);
StringBuffer b= new StringBuffer();
while (m.find()) {
if(m.group(1) != null) m.appendReplacement(b, "SplitHere");
else m.appendReplacement(b, m.group(0));
}
m.appendTail(b);
String replaced = b.toString();
String[] splits = replaced.split("SplitHere");
for (String split : splits) System.out.println(split);
} // end main
} // end Program

      

Link

How to match (or replace) a pattern, except in situations s1, s2, s3 ...

0


source







All Articles