Reputation: 53
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.
Upvotes: 1
Views: 2117
Reputation: 626870
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
Upvotes: 1