[英]RegEx to match elements in a string separated by comma ignoring comma inside double quotes, curly brackets and square brackets
[英]regex to remove comma and space inside of square brackets
您好,我有一個看起來像這樣的文件:
year.", [229, 338], null, [1144, 2371]
year....", null, null, [812]
year:, null, null, [1019, 1028, 2463]
year;, null, [164], [1052]
這就是我想要的文件外觀
year.", 229:338, , 1144:2371
year....", , , 812
year:, , , 1019:1028:2463
year;, , 164, 1052
我已經嘗試過.replaceAll(",[?=[^()]*\\\\]]",":") but this is just replacing all of the commas not the ones inside of the bracket.
看來你想
null
number, number, number
與number:number:number
,換言之替換每個,
其具有數字之前將其與:
[
和]
演示:
String input =
"year.\", [229, 338], null, [1144, 2371]\r\n" +
"year....\", null, null, [812]\r\n" +
"year:, null, null, [1019, 1028, 2463]\r\n" +
"year;, null, [164], [1052]";
String expected =
"year.\", 229:338, , 1144:2371\r\n" +
"year....\", , , 812\r\n" +
"year:, , , 1019:1028:2463\r\n" +
"year;, , 164, 1052";
input = input.replace("null", "")
.replaceAll("(?<=\\d), ", ":")
.replaceAll("\\[|\\]", "");
System.out.println(input.equals(expected));
輸出: true
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.