简体   繁体   English

替换RegEx出现的Java

[英]Replace RegEx occurrences Java

I have a string in Java like this: 我在Java中有这样的字符串:

REF([123],[456],[78]),REF([789],[456],[12]),{111},REF([8069],[8098],[56])

I need to remove all the third occurring digits inside all the REFs. 我需要删除所有REF中所有出现的第三位数字。 Meaning I need to remove [78], [12] and [56] (starting from the second comma till before the closing brackets) from the string so that I get this following output: 这意味着我需要从字符串中删除[78],[12]和[56](从第二个逗号开始直到右括号之前),以便获得以下输出:

REF([123],[456]),REF([789],[456]),{111},REF([8069],[8098])

What should my regex be? 我的正则表达式应该是什么?

String result = subject.replaceAll(
    "(?xi)(      # Match and capture in group 1:\n" +
    "REF\\(      # REF(\n" +
    "\\[\\d+\\], # a number in brackets, comma,\n" +
    "\\[\\d+\\]  # a number in brackets\n" +
    ")           # End of capturing group\n" +
    ",\\[\\d+\\] # Match a comma and a third number in brackets", "$1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM