简体   繁体   English

正则表达式删除方括号内的逗号和空格

[英]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 看来你想

  • remove all null s 删除所有null
  • replace number, number, number with number:number:number , in other words replace each , which has digit before it with : 取代number, number, numbernumber:number:number ,换言之替换每个,其具有数字之前将其与:
  • remove [ 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.

相关问题 正则表达式匹配以逗号分隔的字符串中的元素,忽略双引号、大括号和方括号内的逗号 - RegEx to match elements in a string separated by comma ignoring comma inside double quotes, curly brackets and square brackets 正则表达式不包括方括号 - Regex excluding square brackets 在String regex java中删除逗号后的空格 - Remove space after comma in String regex java 在Java正则表达式的字符类中使用方括号 - Using square brackets inside character class in Java regex 正则表达式将方括号添加到开头/结尾句子中,每个单词也用逗号分隔? - Regex to add square brackets into beginning/end sentence and also each word separate by comma? Java Regex - 拆分逗号分隔列表但排除方括号内的逗号 - Java Regex - Split comma separated list but exclude commas within square brackets 正则表达式返回方括号(但不是括号)之间的字符 - Regex to return the characters between square brackets (but not the brackets) 拆下方括号,然后用卷曲括号取代 - Remove Square Brackets and replace with Curly Brackets 如果不在方括号或括号内,请用逗号分隔字符串 - Split string by comma if not within square brackets or parentheses 正则表达式模式可删除json数组中的方括号和逗号,以便应用MapReduce - Regex pattern to remove square brackets and commas in a json array in order to apply MapReduce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM