有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我想使用regex解析通过Android客户端中的socket接收的消息,并将部分消息放入列表中

这是要分析的消息:

{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中测试了相同的代码,并且运行正常。当我使用toast来显示时,它会给我代码的值,而不是服务的值。是否在某个地方出现了错误,或者regex在Android中的工作方式是否有所不同

谢谢


共 (1) 个答案

  1. # 1 楼答案

    实际上,您可以使用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));
        }
    }
    

    IDEONE demo