简体   繁体   English

Java:拆分逗号分隔的字符串但忽略括号中的逗号

[英]Java: splitting a comma-separated string but ignoring commas in parentheses

I have a string like this:我有一个这样的字符串:

one,two,3,(4,five),six,(seven),(8,9,ten),eleven,(twelve,13,14,fifteen)

the above string should split into:上面的字符串应该分成:

one
two
3
(4,five)
six
(seven)
(8,9,ten)
eleven
(twelve,13,14,fifteen)

The simplest solution to my opinion is to process the input string char-by-char:我认为最简单的解决方案是逐字符处理输入字符串:

public static List<String> split(String input) {
    int nParens = 0;
    int start = 0;
    List<String> result = new ArrayList<>();
    for(int i=0; i<input.length(); i++) {
        switch(input.charAt(i)) {
        case ',':
            if(nParens == 0) {
                result.add(input.substring(start, i));
                start = i+1;
            }
            break;
        case '(':
            nParens++;
            break;
        case ')':
            nParens--;
            if(nParens < 0) 
                throw new IllegalArgumentException("Unbalanced parenthesis at offset #"+i);
            break;
        }
    }
    if(nParens > 0)
        throw new IllegalArgumentException("Missing closing parenthesis");
    result.add(input.substring(start));
    return result;
}

Example:例子:

split("one,two,3,(4,five),six,(seven),(8,9,ten),eleven,(twelve,13,14,fifteen)") ->
[one, two, 3, (4,five), six, (seven), (8,9,ten), eleven, (twelve,13,14,fifteen)]

As a free bonus, this solution also counts nested parentheses if necessary:作为免费奖励,如果需要,此解决方案还会计算嵌套括号:

split("one,two,3,(4,(five,six),seven),eight") ->
[one, two, 3, (4,(five,six),seven), eight]

Also it checks whether parentheses are balanced (every open parenthesis has the corresponding closing one).它还检查括号是否平衡(每个左括号都有相应的右括号)。

有一个相对简单的单行解决方案:

String[] parts = input.split(",(?![^()]*\\))");

here you go... :)干得好... :)

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 *
 * @author S1LENT W@RRIOR
 */
public class Tokenizer {

    public static void main(String... args) {

        List<String> tokens = new ArrayList(); // List to store tokens

        String string = "one,two,3,(4,five),six,(seven),(8,9,ten),eleven,(twelve,13,14,fifteen)"; // input string
        StringTokenizer tokenizer = new StringTokenizer(string, "(),", true); // tokenize your string on the basis of these parentheses 

        while (tokenizer.hasMoreElements()) { // iterate over tokens

            String aToken = (String) tokenizer.nextElement(); // get a token

            if (aToken.equals("(")) { // if token is the begining of a parenthisis
                StringBuilder sb = new StringBuilder(aToken);
                String nextToken = (String) tokenizer.nextElement(); // get next token
                while (!nextToken.equals(")")) { // iterate over next tokens untill you find the ending bracket
                    sb.append(nextToken);
                    nextToken = (String) tokenizer.nextElement();
                }
                sb.append(")");

                tokens.add(sb.toString()); // add to tokens list
            } else if(aToken.equals(",")) { // need this to avoid adding commas
                // do nothing
            } else {
                tokens.add(aToken); // add to tokens list
            }
        }

        for(String aToken: tokens) { // print all tokens
            System.out.println(aToken);
        }
    }
}

Use Regex.使用正则表达式。

    String s = "one,two,3,(4,five),six,(seven),(8,9,ten),eleven,(twelve,13,14,fifteen)";
    Matcher m = Pattern.compile("[^,()]+|\\([^)]*\\)").matcher(s);
    while (m.find())
        System.out.println(m.group());

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

相关问题 Java:拆分逗号分隔的字符串但忽略引号中的逗号 - Java: splitting a comma-separated string but ignoring commas in quotes 正则表达式Java拆分逗号分隔的字符串但忽略引号+大括号+递归括号内的逗号 - regex Java splitting a comma-separated String but ignoring commas within quotes+braces+recursive brackets Java:按逗号分割,并忽略标记中的逗号<p> - Java: splitting by comma and ignoring commas in tag <p> Java正则表达式:拆分逗号分隔的值,但忽略引号中的逗号 - Java regex: split comma-separated values but ignore commas in quotes Java Regex - 拆分逗号分隔列表,但在括号内排除逗号 - Java Regex - split comma separated list, but exclude commas within parentheses 如何在忽略转义逗号的同时拆分逗号分隔的字符串? - How to split a comma separated String while ignoring escaped commas? Java ArrayList<string> 在方括号中形成逗号分隔的字符串</string> - Java ArrayList<String> form comma-separated strings in square brackets 解析包含带引号的逗号和换行符的逗号分隔值 - Parsing comma-separated values containing quoted commas and newlines 将ArrayList的内容输出为逗号分隔的字符串 - Output the contents of an ArrayList as a comma-separated String 逗号分隔的记录到String Array中? - Comma-separated records into String Array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM