替换正则表达式分组 JAVA

huangapple go评论61阅读模式
英文:

Replace regular expression groups JAVA

问题

String finalLine = line.replaceAll("(\\[\\[)(,)(],)(]])", "$1 ,$2,$3\n,$4");

英文:

I'm trying to do a group replacement, I don't know if it's possible to do with just one expression

Text

String line = "[[A, _, _, _, A], [_, A, _, A, _]]";

Expected text

String line = "A _ _ _ A\n_ A _ A _";

Regex: ([[)(,)(],)(]])

Test #1 String finalLine = line.replaceAll("(\\[\\[)(,)(],)(]])", "$1 ,$2,$3\n,$4");

Test #2 String finalLine = line.replaceAll("(\\[\\[)(,)(],)(]])", " ,,\n,"):

Test #3 String finalLine = line..replace ( "]", "]\n" ).replace ( "[[", " " ).replaceAll ( "(])|(,)|(\\[)", "" );

答案1

得分: 1

以下是翻译好的代码部分:

line = line.replaceAll("],", "\n").replaceAll(",", " ").replaceAll("[\\[\\]]", "");
英文:

Try this:

line = line.replaceAll("],", "\n").replaceAll(",", " ").replaceAll("[\\[\\]]", "");

huangapple
  • 本文由 发表于 2020年8月4日 10:58:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63239606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定