谁能帮我用java/scala中的正则表达式来分割逗号上的字符串,而不是用逗号转义。例如:"value1,value2,value3" -> ["value1","value2","value3"]"value1,,6,value2,value3" -> ["value1,,6","value2","value3"]这里逗号既用作分隔符,也用于转义分隔符。多谢了!
"value1,value2,value3" -> ["value1","value2","value3"]
"value1,,6,value2,value3" -> ["value1,,6","value2","value3"]
holgip5t1#
使用((?<!,),(?!(,{2})*+,))。它匹配任何,,它不紧跟逗号,也不紧跟逗号(或奇数个逗号)。
((?<!,),(?!(,{2})*+,))
,
for(String a : "value1,,6,value2,value3,,,val4,,,,val5".split("((?<!,),(?!(,{2})*+,))")) { System.out.println(a.replaceAll(",,", ",")); }
输出:
value1,6 value2 value3 ,val4,,val5 ,,val6
此外,拆分字符串仍然包含“转义”逗号,因此是.replaceAll(",,", ",")。
.replaceAll(",,", ",")
1条答案
按热度按时间holgip5t1#
使用
((?<!,),(?!(,{2})*+,))
。它匹配任何,
,它不紧跟逗号,也不紧跟逗号(或奇数个逗号)。输出:
此外,拆分字符串仍然包含“转义”逗号,因此是
.replaceAll(",,", ",")
。