简体   繁体   English

Java Regex - 拆分逗号分隔列表,但在括号内排除逗号

[英]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:我正在尝试编写将像这样拆分Java字符串的正则表达式:

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.我一直在尝试\,(\()?但这最终也选择了括号中的逗号。

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)

akburg, resurrecting this question for completion because it had another simple solution that wasn't mentioned. akburg,复活这个问题以完成,因为它有另一个没有提到的简单解决方案。 This situation is similar to Match (or replace) a pattern except in situations s1, s2, s3 etc .这种情况类似于匹配(或替换)模式,除了情况 s1、s2、s3 等

Here's our simple regex:这是我们的简单正则表达式:

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

The left side of the alternation matches complete (parentheses) tags.交替的左侧匹配完整的(parentheses)标签。 We will ignore these matches.我们将忽略这些匹配。 The right side matches and captures commas to Group 1, and we know they are the right commas because they were not matched by the expression on the left.右侧将逗号匹配并捕获到第 1 组,我们知道它们是正确的逗号,因为它们没有被左侧的表达式匹配。

This program shows how to use the regex (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

Reference参考

How to match (or replace) a pattern except in situations s1, s2, s3...如何匹配(或替换)模式,除了情况 s1、s2、s3...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Regex - 拆分逗号分隔列表但排除方括号内的逗号 - Java Regex - Split comma separated list but exclude commas within square brackets 正则表达式:逗号分割,但在括号和引号中排除逗号(单双和双) - Regex : Split on comma , but exclude commas within parentheses and quotes(Both single & Double) Java正则表达式:拆分逗号分隔的值,但忽略引号中的逗号 - Java regex: split comma-separated values but ignore commas in quotes Java:拆分逗号分隔的字符串但忽略括号中的逗号 - Java: splitting a comma-separated string but ignoring commas in parentheses 正则表达式Java拆分逗号分隔的字符串但忽略引号+大括号+递归括号内的逗号 - regex Java splitting a comma-separated String but ignoring commas within quotes+braces+recursive brackets 正则表达式在括号中捕获逗号分隔的文本组 [Java] - Regex to capture comma separated groups of text in parentheses [Java] 输出值以逗号分隔并排除最后一个逗号 - Output values to be separated by commas and to exclude the last comma 用引号和引号内的逗号分隔逗号分隔的字符串,并在引号内使用转义引号 - Split comma separated string with quotes and commas within quotes and escaped quotes within quotes Stream Api - 用逗号分割排除嵌套逗号 - Stream Api - split by comma exclude nested commas 逗号分隔的字符串排除双引号中的逗号并分隔相邻的逗号 - String split on comma exclude comma in double quote and split adjacent commas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM