用于嵌套括号的java regexp [英] java regexp for nested parenthesis

查看:86
本文介绍了用于嵌套括号的java regexp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下java字符串:

Consider the following java String:

String input = "a, b, (c, d), e, f, (g, (h, i))";

你能帮我找一个java regexp来获得它的6个部分:

Can you help me to find a java regexp to obtain its 6 parts:

a
b
(c,d)
e
f
(g, (h,i))

这是从基于最外部逗号的原始输入字符串中获得的。

That were obtained from the original input string based in the "most external" commas.

推荐答案

不要尝试将regex用于此类任务,因为Java中的regex不支持递归。最简单的解决方案是编写自己的解析器,它将计算的余额(让我们称之为括号嵌套级别)如果嵌套级别为 0 ,则仅在上拆分。

Don't try to use regex for this kind of task since regex in Java doesn't support recursion. Simplest solution would be writing your own parser which would count balance of ( and ) (lets call it parenthesis nesting level) and will split only on , if nesting level would be 0.

此任务的简单代码(也将在一次迭代中解决此问题)看起来像

Simple code for this task (which will also solve this problem in one iteration) could look like

public static List<String> splitOnNotNestedCommas(String data){
    List<String> resultList = new ArrayList();

    StringBuilder sb = new StringBuilder();
    int nestingLvl = 0;
    for (char ch : data.toCharArray()){
        if (ch == '(') nestingLvl++;
        if (ch == ')') nestingLvl--;
        if (ch == ',' & nestingLvl==0){
            resultList.add(sb.toString().trim());
            sb.delete(0, sb.length());
        }else{
            sb.append(ch);
        }
    }
    if (sb.length()>0)
        resultList.add(sb.toString().trim());

    return resultList;
}

用法:

for (String s : splitOnNotNestedCommas("a, b, (c, d), e, f, (g, (h, i))")){
    System.out.println(s);
}

输出:

a
b
(c, d)
e
f
(g, (h, i))

这篇关于用于嵌套括号的java regexp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆