1

I would like to use regex to parse a message received through a socket in an Android Client and put part of the message in a list.

This is the message to parse:

{Code=1;NumServices=3;Service1=World Weather Online;Link1=http://www.worldweatheronline.com/;Service2=Open Weather Map;Link2=http://openweathermap.org/;Service3=Weather;Link3=http://www.weather.gov/;}

and the method I'm using:

private void parse(String mess) {


String Code="0";
Pattern pattern = Pattern.compile("Code=(.*?);");
Matcher matcher = pattern.matcher(mess);

while (matcher.find()) {
    Code = matcher.group(1);
    Log.d("Matcher", "PATTERN MATCHES! Code parsed "+Code );
 //   System.out.println("Code: "+Code);
}
Log.d("Matcher", "PATTERN MATCHES! Code not parsed "+Code );
if(Code.compareTo("1")==0){

    //   System.out.println("testing the parser");

    //  Pattern pattern1 = Pattern.compile(";CPU=(.*?);Screen");
    Pattern pattern2 = Pattern.compile("NumServices=(.*?);");
    Matcher matcher2 = pattern2.matcher(mess);
    int number=0;
    if (matcher2.find()) {
        String numb = matcher2.group(1);
        this.tester = numb;
        Log.d("Matcher", "PATTERN MATCHES! numb services");
        number = Integer.parseInt(numb);
    }
    else{
        this.tester = "NOT FOUND";
        Log.d("Matcher", "PATTERN MATCHES! match num failed");
    }

    int i;
    for(i=1;i<=number;i++){

        Pattern pattern3 = Pattern.compile(";Service"+i+"=(.*?);");
        Pattern pattern4 = Pattern.compile(";Link"+i+"=(.*?);");

        Matcher matcher3 = pattern3.matcher(mess);
        Matcher matcher4 = pattern4.matcher(mess);

        if (matcher3.find()) {
      //      Log.d("Matcher", "PATTERN MATCHES! services");
            String serv = matcher3.group(1);
     //       this.tester = serv;
            your_array_list.add(serv);

        }


        if (matcher4.find()) {
            Log.d("Matcher", "PATTERN MATCHES! links");
            String link = matcher4.group(1);
            your_array_list2.add(link);
        }

    }

}

}

None of the log.d works so I cannot verify the flow of the code. What's weird is that I tested the same code in Eclipse and it works. When I use toast to display, it gives me the value of Code, but not of Service. Is there an error somewhere or does regex work differently in Android?

Thanks.

5
  • Why didn't you just added a Log.d atte very beginning of the method to check the value of mess ? It would b very useful for you... and for us !!! Commented Jul 19, 2015 at 17:10
  • I used toast to check the value and its the message I send from the server: {Code=1;NumServices=3;Service1=World Weather Online;Link1=worldweatheronline.com/;Service2=Open Weather Map;Link2=openweathermap.org/;Service3=Weather;Link3=http://…;} Commented Jul 19, 2015 at 17:13
  • Please clarify what you mean when you say you cannot display the value of Service. Do you mean Service1, Service2, Service3, or NumServices? Commented Jul 19, 2015 at 17:14
  • I meant Service1, Service2 etc .. but I can't actually check the NumServices neither Commented Jul 19, 2015 at 17:18
  • It was obvious that your code wasn't called at all, and I wonder why you didn't want to add the requested Log.d() at first, just to be sure. You should learn to always expect the worse (and the most unexpected) when you debug your code... Commented Jul 19, 2015 at 21:03

1 Answer 1

1

You can actually use 1 regex to capture all pertinent data:

Code=([^;]*);NumServices=([^;]*);|Service(\d+)=([^;]*);Link\d+=([^;]*);

Here is a sample code:

String str = "{Code=1;NumServices=3;Service1=World Weather Online;Link1=http://www.worldweatheronline.com/;Service2=Open Weather Map;Link2=http://openweathermap.org/;Service3=Weather;Link3=http://www.weather.gov/;}";
Pattern ptrn = Pattern.compile("Code=([^;]*);NumServices=([^;]*);|Service(\\d+)=([^;]*);Link\\d+=([^;]*);");
Matcher matcher = ptrn.matcher(str);
while (matcher.find()) {
    if (matcher.group(1) != null) {
       System.out.println("Code: " + matcher.group(1));
       System.out.println("NumServices: " + matcher.group(2));
    }
    else if (matcher.group(1) == null && matcher.group(2) == null) {
       System.out.println("Service #: " + matcher.group(3));
       System.out.println("Service Name: " + matcher.group(4));
       System.out.println("Link: " + matcher.group(5));
    }
}

See IDEONE demo

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

1 Comment

I just found out I had a problem in the way I was calling the method. Both regex work, but I think yours is better and gives a more optimized code. Thank you guys !!

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.