请问各位:
为什么我的程序老是会打印出三组同样的信息如下:(怎么样才能只打印出一组呢)
http://news.bai-du.com/ns
http://news.aa.com/ns
http://news.bai-du.com/ns
http://news.aa.com/ns
http://news.bai-du.com/ns
http://news.aa.com/ns
我的源代码如下
import java.util.regex.*;public class Test {
String a =
 ">aa<faaaaa<a href=\"http://news.bai-du.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>aaaaaaaa";
Test() {
}
public static void main(String[] args) {
String a =
 ">aa<faaaaa<a href=\"http://news.bai-du.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>aaaaaaaa";
a += ">aa<faaaaa<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>aaaaaaaa"; String[] str = a.split("<a");
for(int i = 0; i < str.length; i ++) {
Matcher m1 = Pattern.compile("http://\\w+.\\S+.\\w+/\\w+()").matcher(a);
while(m1.find()) {

for (int j = 0; j<m1.groupCount(); j++) {
System.out.println (m1.group(j));
}
}
}
}
}

解决方案 »

  1.   

    for(int i = 0; i < str.length; i ++) 这是干嘛的?
    str.length=3 自然打印3遍了
      

  2.   

    public class Test {
    String a =
     ">aa<faaaaa<a href=\"http://news.bai-du.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>aaaaaaaa";
    Test() {
    }
    public static void main(String[] args) {
    String a =
     ">aa<faaaaa<a href=\"http://news.bai-du.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>aaaaaaaa";
    a += ">aa<faaaaa<a href=\"http://news.aa.com/ns?cl=2&rn=20&tn=news&word=a\">新闻</a>aaaaaaaa";

    Matcher m1 = Pattern.compile("http://\\w+.\\S+.\\w+/\\w+()").matcher(a);
    while(m1.find()) { for (int j = 0; j<m1.groupCount(); j++) {
    System.out.println (m1.group(j));
    } }
    }
    }
    自己 做的 都 不 知道 意思 。
      

  3.   

    你循环了3次 所以3次输出
    还有你知道这3个之间的联系吗?
    Matcher m1 = Pattern.compile("http://\\w+.\\S+.\\w+/\\w+()").matcher(a);
    m1.groupCount(); 
    m1.group(j));