---------------------------------------------------------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------------------------------------------------------------