如何在逗号上用一定的条件在Java中拆分字符串

时间:2018-07-20 10:46:47

标签: java string split comma

仅当逗号不在()内且不在另一个包含逗号的查询内时,才可以将字符串拆分为以下逗号。

例如

{ORDER_BY=TABLE1.ID, WHERE_CLAUSE=TABLE1.QUANTITY > 100, WHERE_CLAUSE_2=AND TABLE1.TYPE IN (1,2,3), SUB_QUERY=UNION ALL SELECT XXX AS ABC, COLUMN1 AS DESCRIPTION, COLUMN2 AS NAME, COLUMN3 AS SOMETHINE_ELSE, SUM(TABLE1.QUANTITY) AS TOTAL_SUM, LEFT JOIN TABLE2 T2 ON T2.ID = T1.T2ID GROUP BY A,B,C,WHERE_CLAUSE_3=}

我想要上面的输出:

PAIR1: ORDER_BY=TABLE1.ID

PAIR2: WHERE_CLAUSE=TABLE1.QUANTITY > 100

PAIR3: WHERE_CLAUSE_2=AND TABLE1.TYPE IN (1,2,3)

PAIR4: SUB_QUERY=UNION ALL SELECT XXX AS ABC, COLUMN1 AS DESCRIPTION, COLUMN2 AS NAME, COLUMN3 AS SOMETHINE_ELSE, SUM(TABLE1.QUANTITY) AS TOTAL_SUM, LEFT JOIN TABLE2 T2 ON T2.ID = T1.T2ID GROUP BY A,B,C

PAIR5: WHERE_CLAUSE_3=

我知道我可以用

分割字符串
String[] keyValuePairs = reportParams.split(",(?![^()]*+\\))"); 

仅当逗号不在普通方括号中时才会拆分,因此不会向上拆分PAIR3“(1,2,3)”

但是我如何不将查询中所有逗号分隔成PAIR4?

当前它给了我

PAIR1: ORDER_BY=TABLE1.ID

PAIR2: WHERE_CLAUSE=TABLE1.QUANTITY > 100

PAIR3: WHERE_CLAUSE_2=AND TABLE1.TYPE IN (1,2,3)

PAIR4: SUB_QUERY=UNION ALL SELECT XXX AS ABC

PAIR5: COLUMN1 AS DESCRIPTION

PAIR6: COLUMN2 AS NAME

PAIR7: COLUMN3 AS SOMETHINE_ELSE

PAIR8: SUM(TABLE1.QUANTITY) AS TOTAL_SUM

PAIR9: LEFT JOIN TABLE2 T2 ON T2.ID = T1.T2ID GROUP BY A

PAIR10: B

PAIR11: C

PAIR12: WHERE_CLAUSE_3=

我无法更改原始字符串的存储方式,因为这意味着我需要手动更改数百个针对iReports的报表查询。

0 个答案:

没有答案