3

我想通过正则表达式进行 java 拆分。当字符串不在单引号或方括号中时,我想在每个逗号上拆分字符串。例子:

Hello, 'my,',friend,(how ,are, you),(,)


 should give:
    hello
    my,
    friend
    how, are, you
    ,

我试过这个:

(?i),(?=([^\'|\(]*\'|\([^\'|\(]*\'|\()*[^\'|\)]*$)

但我无法让它工作(我通过http://java-regex-tester.appspot.com/进行了测试)

有任何想法吗?

4

5 回答 5

6

嵌套的括号不能被正则表达式分割。手动拆分它们更容易。

public static List<String> split(String orig) {
    List<String> splitted = new ArrayList<String>();
    int nextingLevel = 0;
    StringBuilder result = new StringBuilder();
    for (char c : orig.toCharArray()) {
        if (c == ',' && nextingLevel == 0) {
            splitted.add(result.toString());
            result.setLength(0);// clean buffer
        } else {
            if (c == '(')
                nextingLevel++;
            if (c == ')')
                nextingLevel--;
            result.append(c);
        }
    }
    // Thanks PoeHah for pointing it out. This adds the last element to it.
    splitted.add(result.toString());
    return splitted;
}

希望这可以帮助。

于 2013-04-19T15:38:30.497 回答
3

java CSV 解析器库比正则表达式更适合此任务:http: //sourceforge.net/projects/javacsv/

于 2013-04-19T15:30:31.687 回答
1

假设没有嵌套(),您可以拆分

",(?=(?:[^']*'[^']*')*[^']*$)(?=(?:[^()]*\\([^()]*\\))*[^()]*$)"

'只有当字符串中的前面是偶数个和括号对 时,它才会以逗号分隔。

这是一个脆弱的解决方案,但它可能已经足够好了。

于 2013-04-19T15:52:43.330 回答
1

正如@Balthus 的一些评论和回答一样,这最好在CSV Parser中完成。您确实需要做一些smart RexEx replacement准备输入字符串以进行解析。考虑这样的代码:

String str = "Hello, 'my,',friend,(how ,are, you),(,)"; // input string

// prepare String for CSV parser: replace left/right brackets OR ' by a "
CsvReader reader = CsvReader.parse(str.replaceAll("[(')]", "\""));
reader.readRecord(); // read the CSV input
for (int i=0; i<reader.getColumnCount(); i++)
   System.out.printf("col[%d]: [%s]%n", i, reader.get(i));

输出

col[0]: [Hello]
col[1]: [my,]
col[2]: [friend]
col[3]: [how ,are, you]
col[4]: [,]
于 2013-04-19T16:56:31.797 回答
0

我还需要在引号和括号之外用逗号分隔。

在搜索了关于 SO 的所有相关答案后,我意识到在这种情况下需要一个词法分析器,我为自己编写了一个通用实现。它支持分隔符、多个引号和多个括号作为正则表达式。

public static List<String> split(String string, String regex, String[] quotesRegex, String[] leftBracketsRegex,
                                 String[] rightBracketsRegex) {

    if (leftBracketsRegex.length != rightBracketsRegex.length) {
        throw new IllegalArgumentException("Bracket count mismatch, left: " + leftBracketsRegex.length + ", right: "
                + rightBracketsRegex.length);
    }

    // Prepare all delimiters.
    String[] delimiters = new String[1 + quotesRegex.length + leftBracketsRegex.length + rightBracketsRegex.length];
    delimiters[0] = regex;
    System.arraycopy(quotesRegex, 0, delimiters, 1, quotesRegex.length);
    System.arraycopy(leftBracketsRegex, 0, delimiters, 1 + quotesRegex.length, leftBracketsRegex.length);
    System.arraycopy(rightBracketsRegex, 0, delimiters, 1 + quotesRegex.length + leftBracketsRegex.length,
            rightBracketsRegex.length);

    // Build delimiter regex.
    StringBuilder delimitersRegexBuilder = new StringBuilder("(?:");
    boolean first = true;
    for (String delimiter : delimiters) {
        if (delimiter.endsWith("\\") && !delimiter.endsWith("\\\\")) {
            throw new IllegalArgumentException("Delimiter contains trailing single \\: " + delimiter);
        }
        if (first) {
            first = false;
        } else {
            delimitersRegexBuilder.append("|");
        }
        delimitersRegexBuilder
                .append("(")
                .append(delimiter)
                .append(")");
    }
    delimitersRegexBuilder.append(")");
    String delimitersRegex = delimitersRegexBuilder.toString();

    // Scan.
    int pendingQuoteIndex = -1;
    Deque<Integer> bracketStack = new LinkedList<>();
    StringBuilder pendingSegmentBuilder = new StringBuilder();
    List<String> segmentList = new ArrayList<>();
    Matcher matcher = Pattern.compile(delimitersRegex).matcher(string);
    int matcherIndex = 0;
    while (matcher.find()) {
        pendingSegmentBuilder.append(string.substring(matcherIndex, matcher.start()));
        int delimiterIndex = -1;
        for (int i = 1; i <= matcher.groupCount(); ++i) {
            if (matcher.group(i) != null) {
                delimiterIndex = i - 1;
                break;
            }
        }
        if (delimiterIndex < 1) {
            // Regex.
            if (pendingQuoteIndex == -1 && bracketStack.isEmpty()) {
                segmentList.add(pendingSegmentBuilder.toString());
                pendingSegmentBuilder.setLength(0);
            } else {
                pendingSegmentBuilder.append(matcher.group());
            }
        } else {
            delimiterIndex -= 1;
            pendingSegmentBuilder.append(matcher.group());
            if (delimiterIndex < quotesRegex.length) {
                // Quote.
                if (pendingQuoteIndex == -1) {
                    pendingQuoteIndex = delimiterIndex;
                } else if (pendingQuoteIndex == delimiterIndex) {
                    pendingQuoteIndex = -1;
                }
                // Ignore unpaired quotes.
            } else if (pendingQuoteIndex == -1) {
                delimiterIndex -= quotesRegex.length;
                if (delimiterIndex < leftBracketsRegex.length) {
                    // Left bracket
                    bracketStack.push(delimiterIndex);
                } else {
                    delimiterIndex -= leftBracketsRegex.length;
                    // Right bracket
                    int topBracket = bracketStack.peek();
                    // Ignore unbalanced brackets.
                    if (delimiterIndex == topBracket) {
                        bracketStack.pop();
                    }
                }
            }
        }
        matcherIndex = matcher.end();
    }
    pendingSegmentBuilder.append(string.substring(matcherIndex, string.length()));
    segmentList.add(pendingSegmentBuilder.toString());

    while (segmentList.size() > 0 && segmentList.get(segmentList.size() - 1).isEmpty()) {
        segmentList.remove(segmentList.size() - 1);
    }

    return segmentList;
}
于 2015-04-25T11:03:37.130 回答