个人记录——java处理文本中的括号

---------------------------------------------------------2023/7/2------------------------------------------------------------

java暂时没有办法进行正则递归,但是针对文本中的括号需要进行处理,比如处理复杂SQL,个人尝试通过模仿先序遍历的那种感觉,尝试写了一种方式遍历出所有的【括号对】在字符串中的【索引组】,代码如下:

public static void main(String[] args) {
    String input = "((1+2*(5+6))*(3+4))";
    ArrayList<Integer> startIndexList = new ArrayList<Integer>();
    ArrayList<Integer> endIndexList = new ArrayList<Integer>();
    int switchButton = 1;
    for (int i = 0; i < input.length() ; i++)
    {
        if (input.charAt(i)=='(')
        {
            startIndexList.add(i);
            endIndexList.add(null);
            switchButton = 1;
        }
        else if (input.charAt(i)==')')
        {
            if (switchButton == 1)
            {
                endIndexList.set(startIndexList.size()-1,i);
            }
            else if(switchButton == 0)
            {
                for(int j = startIndexList.size() - 1;j>=0;j--)
                {
                    if (endIndexList.get(j) == null)
                    {
                        endIndexList.set(j,i);
                        break;
                    }
                }
            }
            switchButton = 0 ;
        }
    }
}

留作个人记录,待思考怎么处理sql中的层级

---------------------------------------------------------2023/7/2------------------------------------------------------------

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值