当前位置: 代码迷 >> java >> 使用正则表达式在Android中解析
  详细解决方案

使用正则表达式在Android中解析

热度:35   发布时间:2023-07-31 12:05:38.0

我想使用正则表达式来解析通过Android客户端中的套接字接收到的消息,并将部分消息放入列表中。

这是要解析的消息:

{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/;}

和我使用的方法:

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

    }

}

}

log.d因此我无法验证代码流。 奇怪的是,我在Eclipse中测试了相同的代码,并且可以正常工作。 当我使用烤面包来显示时,它给了我Code的值,但没有Service的值 某个地方是否有错误,或者regex在Android中的工作方式是否有所不同?

谢谢。

实际上,您可以使用1个正则表达式来捕获所有相关数据:

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

这是一个示例代码:

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