<body onload="init();">
<div id="temp" style="">
####PostList?modelclassID=3&deptID=1&current_page=1&page_rows=1&forum_id=5####
####PostList?modelclassID=3&deptID=2&current_page=1&page_rows=2&forum_id=4####
####PostList?modelclassID=3&deptID=3&current_page=1&page_rows=3&forum_id=4####
####PostList?modelclassID=3&deptID=4&current_page=1&page_rows=4&forum_id=4####
####PostList?modelclassID=3&deptID=5&current_page=1&page_rows=5&forum_id=45147####
</div></body>以上是我在网页中的一段代码,这个网页是用servlet从HTML文件中读取的,用正则表达式匹配“####PostList?modelclassID=3&deptID=1&current_page=1&page_rows=1&forum_id=5####”这个字符串,应该匹配出5个字符串,
这是我用的正则表达式“(####\\w+\\?(\\w+=\\w+&?)+####)”,怎么样不用循环,一次匹配出这5个字符串啊

解决方案 »

  1.   


            import java.util.regex.Matcher;
            import java.util.regex.Pattern;        ...        String str = "####PostList?modelclassID=3&deptID=1&current_page=1&page_rows=1&forum_id=5####"
                    + "####PostList?modelclassID=3&deptID=2&current_page=1&page_rows=2&forum_id=4####"
                    + "####PostList?modelclassID=3&deptID=3&current_page=1&page_rows=3&forum_id=4####"
                    + "####PostList?modelclassID=3&deptID=4&current_page=1&page_rows=4&forum_id=4####"
                    + "####PostList?modelclassID=3&deptID=5&current_page=1&page_rows=5&forum_id=45147####";
            String regex = "####\\w+\\?(\\w+=\\w+&?)+####";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(str);
            while (matcher.find())
                System.out.println(matcher.group());
      

  2.   

    (####.*?####)+
    不知道这样行不行,没装jdk
      

  3.   

    "####\\w+\\?(\\w+=\\w+&?)+####\s+####\\w+\\?(\\w+=\\w+&?)+####\s+####\\w+\\?(\\w+=\\w+&?)+####\\s+####\\w+\\?(\\w+=\\w+&?)+####\\s+####\\w+\\?(\\w+=\\w+&?)+####"
      

  4.   

    上面好像漏写了一个转义字符\
    ####\\w+\\?(\\w+=\\w+&?)+####\\s+####\\w+\\?(\\w+=\\w+&?)+####\\s+####\\w+\\?(\\w+=\\w+&?)+####\\s+####\\w+\\?(\\w+=\\w+&?)+####\\s+####\\w+\\?(\\w+=\\w+&?)+####
      

  5.   

    不知道你“不使用循环”的限制是怎么 & 为什么,下面是对上面代码的改进。        String regex = "####(.*?)####";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(str);
            while (matcher.find())
                System.out.println(matcher.group(1));
      

  6.   

    如果你想写死代码,也就是不使用循环,代码如下:        String r = "####(.*?)####\\s*####(.*?)####\\s*####(.*?)####\\s*####(.*?)####\\s*####(.*?)####";
            Pattern p = Pattern.compile(r);
            Matcher m = p.matcher(str);
            m.find();
            System.out.println(m.group(1));
            System.out.println(m.group(2));
            System.out.println(m.group(3));
            System.out.println(m.group(4));
            System.out.println(m.group(5));
      

  7.   

    啊,楼主你发了那么多啊,在Java Web版帮你解决了,为啥不结帖呢?http://topic.csdn.net/u/20080108/13/6562c9b4-7a28-4a71-99e7-10a44d423f8b.html