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.

2
  • Why not just use ], as your delimiter? For extra credit, use a regular expression that splits on the , only when preceded by the ]. Commented Apr 10, 2014 at 17:23
  • To achieve what @Gabe has suggested, take a look at the "Lookarounds" section in the Stack Overflow Regular Expressions FAQ, particularly at (?<=...):positive lookbehinds. Commented Apr 10, 2014 at 17:38

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Very nice. Thanks for "Pattern" class. I wasn't aware of it
You welcome. The API for regex in java is not ideal but it does the job. Also check this tutorial.
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)

Comments

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 ']'

Comments

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.

Comments

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);   
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.