[英]regex to remove comma and space inside of square brackets
Hello I have a file that looks like this: 您好,我有一个看起来像这样的文件:
year.", [229, 338], null, [1144, 2371]
year....", null, null, [812]
year:, null, null, [1019, 1028, 2463]
year;, null, [164], [1052]
This what I would like the file to look like 这就是我想要的文件外观
year.", 229:338, , 1144:2371
year....", , , 812
year:, , , 1019:1028:2463
year;, , 164, 1052
I have tried .replaceAll(",[?=[^()]*\\\\]]",":") but this is just replacing all of the commas not the ones inside of the bracket.
我已经尝试过
.replaceAll(",[?=[^()]*\\\\]]",":") but this is just replacing all of the commas not the ones inside of the bracket.
It looks like you want to 看来你想
null
s null
number, number, number
with number:number:number
, in other words replace each ,
which has digit before it with :
number, number, number
与number:number:number
,换言之替换每个,
其具有数字之前将其与:
[
and ]
[
和]
Demo: 演示:
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));
Output: true
. 输出:
true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.