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 ,.

aioobe
  • 413,195
  • 112
  • 811
  • 826
Tilsight
  • 85
  • 6
  • 1
    Is `[a24, a23]` another array or just a string `"[a24, a23]"`? – Lino May 21 '19 at 13:51
  • 2
    have you tried anything? – Reimeus May 21 '19 at 13:52
  • Please turn to the [help] 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 '19 at 13:53
  • Sounds quite similar to https://stackoverflow.com/q/7804335/276052 – aioobe May 21 '19 at 13:53
  • What exactly do you want? If you want to split it using `, `, you can do `arr.split(", ")` – dan1st May 21 '19 at 13:54
  • @Tilsight, please have a look at the link I posted further up. There I've posted a solution for how to split `a b "c d" e f` into `"a"`, `"b"`, `"c d"`, `"e"`, `"f"`, i.e. it should be the same as your problem, except you use `,` instead of space as separator, and `[`...`]` instead of `"`...`"` as grouping characters. – aioobe May 21 '19 at 14:00
  • 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 '19 at 14:02
  • thanks, it works, and also thanks for the formatting suggestions. – Tilsight May 22 '19 at 06:22

2 Answers2

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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
-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));
    }
Sulaiman Triarjo
  • 333
  • 3
  • 15