1

I have an input string of the following format:

Message:id1:[label1:label2....:labelN]:id2:[label1:label2....:labelM]:id3:[label1:label2....:labelK]...

It is basically ids associated with sets of labels. There can be an arbitrary number of ids and labels associated with those ids.

I want to be able to parse this string and generate a HashMap of the form id->labels for quick look up later.

I was wondering what would be the most efficient way of parsing this message in java?

2
  • 2
    A series of String#split() should do the trick. Commented May 5, 2013 at 18:35
  • That looks a lot like JSON. Commented May 5, 2013 at 18:49

3 Answers 3

2

Something like this should work for you:

String str = "Message:id1:[label1:label2:labelN]:id2:[label1:label2:labelM]:id3:[label1:label2:labelK]";
Pattern p = Pattern.compile("([^:]+):\\[([^\\]]+)\\]");
Matcher m = p.matcher(str.substring(8));
Map<String, List<String>> idmap = new HashMap<String, List<String>>(); 
while (m.find()) {
    List<String> l = new ArrayList<String>();
    String[] tok = m.group(2).split(":");
    for (String t: tok)
        l.add(t);
    idmap.put(m.group(1), l);
}
System.out.printf("IdMap %s%n", idmap);

Live Demo: http://ideone.com/EoieUt

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

1 Comment

That is simply awesome. I couldn't come up with a regexp for it. Thanks.
1

Consider using Guava's Multimap

If you take the string you gave:

Message:id1:[label1:label2....:labelN]:id2:[label1:label2....:labelM]:id3:[label1:label2....:labelK]

And do String.split("]"), You get:

Message:id1:[label1:label2....:labelN
:id2:[label1:label2....:labelM
:id3:[label1:label2....:labelK

If you loop through each of those, splitting on [, you get:

Message:id1:     label1:label2....:labelN
:id2:            label1:label2....:labelM
:id3:            label1:label2....:labelK

Then, you can parse the id name out of the first element in the String[], and the labelname out of the second element in the String, and store that in your Multimap.

If you don't want to use Guava, you can also use a Map<String, List<String>>

Comments

0

Following code will serve your requirement.

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {

        public static void main(String[] args){
           String msg = "id1:[label1:label2]:id2:[label1:label2:label3]:id3:[label1:label2:label3:label4]";
           Pattern pattern = Pattern.compile("id");
            HashMap<String,String> kv = new HashMap<String,String>();
            Matcher m = pattern.matcher(msg);

            int prev = -1;
            int next = -1;
            int start = -1;
            int end = -1;

            String subMsg = "";
            while (m.find()){
                if(prev == -1){
                    prev = m.end();
                }
                else
                {
                    next = m.end();
                    start = prev;
                    end =   next;
                    subMsg = msg.substring(start,end);
                    kv.put(String.valueOf(subMsg.charAt(0)),subMsg.substring(subMsg.indexOf("["),subMsg.lastIndexOf("]")+1));
                    prev = next;
                }
            }
            subMsg = msg.substring(next);
            kv.put(String.valueOf(subMsg.charAt(0)),subMsg.substring(subMsg.indexOf("["),subMsg.lastIndexOf("]")+1));

            System.out.println(kv);
        }
}

Output : {3=[label1:label2:label3:label4], 2=[label1:label2:label3], 1=[label1:label2]}

Live Demo : http://ideone.com/HM7989

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.