Line breaks on {} & []

I'm kind of new to Java. I would like to know if there is an even simpler but more efficient way to implement the following line breaks. I tried with pattern and match, but not really the way I want.

"{1,24,5,[8,5,9],7,[0,1]}" 

to split into:

 1 24 5 [8,5,9] 7 [0,1] 

This is completely wrong code, but I post it anyway:

  String str = "{1,24,5,[8,5,9],7,[0,1]}"; str= str.replaceAll("\\{", ""); str= str.replaceAll("}", ""); Pattern pattern = Pattern.compile("\\[(.*?)\\]"); Matcher matcher = pattern.matcher(str); String[] test = new String[10]; // String[] _test = new String[10]; int i = 0; String[] split = str.split(","); while (matcher.find()) { test[i] = matcher.group(0); String[] split1 = matcher.group(0).split(","); // System.out.println(split1[i]); for (int j = 0; j < split.length; j++) { if(!split[j].equals(test[j])&&((!split[j].contains("\\["))||!split[j].contains("\\]"))){ System.out.println(split[j]); } } i++; } } 

Given the specified String format, you can use the format {a, b, [c, d, e], ...}. I want to enlist all the contents, but those indicated in square brackets should be designated as one element (for example, an array).

+4
source share
1 answer

It works:

  public static void main(String[] args) { customSplit("{1,24,5,[8,5,9],7,[0,1]}"); } static void customSplit(String str){ Pattern pattern = Pattern.compile("[0-9]+|\\[.*?\\]"); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group()); } } 

Sets output

 1 24 5 [8,5,9] 7 [0,1] 
+6
source

Source: https://habr.com/ru/post/1487886/


All Articles