简体   繁体   中英

Replace regular expression groups JAVA

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 ( "(])|(,)|(\\[)", "" );

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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