1

I'm attempting to split a string like this one:

[a05, [a24, a23], [b08, b09], c26, c30, a22, a13, m06]

into the following parts:

a05
[a24, a23]
[b08, b09]
c26
c30
a22
a13
m06

That is, split on , but treat [...] as one token, even if it contains a ,.

8
  • 1
    Is [a24, a23] another array or just a string "[a24, a23]"?
    – Lino
    May 21, 2019 at 13:51
  • 2
    have you tried anything?
    – Reimeus
    May 21, 2019 at 13:52
  • Please turn to the help center to learn how/what to ask here. Just dropping requirements "this is what I want" isn't appreciated. When you try something yourself, and you get stuck with a specific problem, we will gladly help. But please understand that this place is not intended to give guidance with the possibly many steps required to get you from your vision to a working program.
    – GhostCat
    May 21, 2019 at 13:53
  • Sounds quite similar to stackoverflow.com/q/7804335/276052
    – aioobe
    May 21, 2019 at 13:53
  • 1
    @GhostCat, what's the point?! I tried looping but I don't know how to append results to an array, I tried split but it doesn't respect my [...] grouping, I'm confused because I can't modify strings, bla bla bla... How does that help a future reader with a similar problem coming here from google?
    – aioobe
    May 21, 2019 at 14:02

2 Answers 2

6

Here's one approach using regular expressions:

String input = "[a05, [a24, a23], [b08, b09], c26, c30, a22, a13, m06]";

// Strip outer [...]
String content = input.substring(1, input.length() - 1);

List<String> parts = new ArrayList<>();
Matcher m = Pattern.compile("\\[.*?\\]|[^\\[, ]+").matcher(content);
while (m.find()) {
    parts.add(m.group());
}
parts.forEach(System.out::println);

Output:

a05
[a24, a23]
[b08, b09]
c26
c30
a22
a13
m06

Regex break down:

  • \[.*?\] -- something on the form [...]
  • | -- or
  • [^\[, ]+ -- one or more characters that are not [, , or space.

Perhaps I took your example too literally. Feel free to expand your example with more complicated cases if the above doesn't work out.

A note on regular expressions

Note that regular expressions are quite limited in what they can express, and only suitable when input is fairly predictable. Should you discover the need for arbitrary nesting of brackets [...[...]...] or similar cases, you have to do more work. The "next step" would probably be to loop / parse input "by hand" or to write a context free grammar and use a parser generator.

0
-2

import arrayList first

import java.util.ArrayList;

we need this to the last result transformation

boolean check = false;
    ArrayList<String> result = new ArrayList<String>();
    String yourString =  "[a05, [a24, a23], [b08, b09], c26, c30, a22, a13, m06]";
    // we need to remove the first char "[" and the last char "]"
    yourString = yourString.substring(0, yourString.length() - 1);
    yourString = yourString.substring(1, yourString.length());
    // and then, we need to split
    String[] parts = yourString.split(", ");
    String temp = "";
    for(int i =0;i<parts.length;i++){
      if(parts[i].contains("[")){
        check = true;
        temp += parts[i] +", ";
      }
      else if(check == false){
        result.add(parts[i]);
      }

      else if(parts[i].contains("]")){
        temp += parts[i];
        result.add(temp);
        temp = "";
        check = false;
      }
      else if(check == true){
        temp += parts[i]+", ";
      }
    }
    System.out.println(result.size());
    for(int i =0;i<result.size();i++){
      System.out.println(result.get(i));
    }
5
  • Minor detail, str.length() should be yourString.length() for both lines.
    – Nexevis
    May 21, 2019 at 13:58
  • thanks for your correction May 21, 2019 at 13:59
  • 1
    Also this splits the inner array from each other and does not output the format he wants it. He wants to keep [a24, a23] together for example, currently it will be split by the comma in your code.
    – Nexevis
    May 21, 2019 at 14:00
  • And: split isn't a keyword. It is the name of a method. Keywords are things like "for", "public", "class", ...
    – GhostCat
    May 21, 2019 at 14:05
  • I have finished. thanks to you all May 21, 2019 at 14:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.