0

I need to split a string based on delimiters and assign it to an object. I am aware of the split function, but I am unable to figure how to do it for my particular string.

The object is of the format:

class Selections{
int n;
ArrayList<Integer> choices;
}

The string is of the form :

1:[1,3,2],2:[1],3:[4,3],4:[4,3]

where:

1:[1,3,2] is an object with n=1 and Arraylist should have numbers 1,2,3. 
2:[1] is an object with n=2 and Arraylist should have number 1

and so on .

I cannot use split with "," as delimiter because both individual objects and the elements within [] are separated by ",".

Any ideas would be appreciated.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
CuriousCoder
  • 1,582
  • 5
  • 28
  • 55
  • Why not just use `],` as your delimiter? For extra credit, use a regular expression that splits on the `,` only when preceded by the `]`. – Gabe Apr 10 '14 at 17:23
  • To achieve what @Gabe has suggested, take a look at the "Lookarounds" section in the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496), particularly at [`(?<=...)`:positive lookbehinds](http://stackoverflow.com/a/11197672). – aliteralmind Apr 10 '14 at 17:38

5 Answers5

1

How about using "]," as delimiter? If your structure is strictly like you said, it should be able to identify and split.

(Sorry, I want to leave it as comment, but my reputation does not allow)

Robin
  • 99
  • 3
1

You could use a regex to have a more robust result as follows:

String s = "1:[1,3,2],2:[1],3:[4,3],4:[4,3],5:[123,53,1231],123:[54,98,434]";
// commented one handles white spaces correctly
//Pattern p = Pattern.compile("[\\d]*\\s*:\\s*\\[((\\d*)(\\s*|\\s*,\\s*))*\\]");
Pattern p = Pattern.compile("[\\d]*:\\[((\\d*)(|,))*\\]");
Matcher matcher = p.matcher(s);

while (matcher.find())
  System.out.println(matcher.group());

The regex can probably be tuned to be more accurate (e.g., handling white spaces) but it works fine on the example.

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
0

You will need to perform multiple splits.

  1. Split with the delimiter "]," (as mentioned in other comments and answers).
  2. For each of the resulting strings, split with the delimiter ":[".
  3. you will need to cleanup the last entry (from the split in step 1), because it will end with ']'
DwB
  • 37,124
  • 11
  • 56
  • 82
0

I have no idea how to use a build-in function for this. I would just write my own split method:

private List<Sections> split(String s){
    private List<Sections> sections = new ArrayList<>();
    private boolean insideBracket = false;
    private int n = 0;
    private List<Integer> ints = new ArrayList<>();

    for (int i = 0; i < s.length(); i++){
        char c = s.charAt(i); 
        if(!insideBracket && !c.equals(':')){
            n = c.getNumericValue();
        } else if(c.equals('[')){
            insideBracket = true;
        } else if (c.equals(']')){
            insideBracket = false;
            sections.add(new Section(n, ints));
            ints = new ArrayList();
        } else if(insideBracket && !c.equals(',')){
            ints.add(c.getNumericValue());
        }
    }
}

you probably need to modify that a little bit. Right now it dont works if a number has multiple digits.

Peter Lamby
  • 285
  • 2
  • 7
0

Try this

while(true){
        int tmp=str.indexOf("]")+1;
        System.out.println(str.substring(0,tmp));
        if(tmp==str.length())
            break;
        str=str.substring(tmp+1);   
    }
gifpif
  • 4,507
  • 4
  • 31
  • 45