簡體   English   中英

正則表達式刪除方括號內的逗號和空格

[英]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, numbernumber: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM