在下面段字符串中,我要提取除了含有 字符串“百度”之外的所有超链接。 </p><p style=height:14px> <a href=http://jingjia.baidu.com>企业推广</a> | <a href=http://top.baidu.com>搜索风云榜</a> | <a href=/home.html>关于百度</a> | <a href=http://ir.baidu.com>About Baidu</a></p><p id=b>&copy;2008 Baidu <a href=http://www.baidu.com/duty>使用百度前必读</a> <a href=http://www.miibeian.gov.cn target=_blank>京ICP证030173号</a> <a href=http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412><img src=http://gimg.baidu.com/img/gs.gif></a></p></center></body></html><!--543ff95f18f36b11-->例如 我需要的是
<a href=http://jingjia.baidu.com>企业推广</a>但是不需要
<a href=/home.html>关于百度</a>
<a href=http://www.baidu.com/duty>使用百度前必读</a>
等含有 百度关键字的。应该怎么写呢?像<a href=http://jingjia.baidu.com>企业推广</a>我可以写成
<a .*?/a>但是加上要排除的关键字后就不知道了。
请教达人?

解决方案 »

  1.   

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test {    public static void main(String args[]) {
            String str = "</p><p style=height:14px>" +
                    "<a href=http://jingjia.baidu.com>企业推广</a> | " +
                    "<a href=http://top.baidu.com>搜索风云榜</a> | " +
                    "<a href=/home.html>关于百度</a> | " +
                    "<a href=http://ir.baidu.com>About Baidu</a>" +
                    "</p><p id=b>&copy;2008 Baidu " +
                    "<a href=http://www.baidu.com/duty>使用百度前必读</a> " +
                    "<a href=http://www.miibeian.gov.cn target=_blank>京ICP证030173号</a> " +
                    "<a href=http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412>" +
                    "<img src=http://gimg.baidu.com/img/gs.gif></a>" +
                    "</p></center></body></html><!--543ff95f18f36b11-->";
            Pattern pattern = Pattern.compile("<a[^>]*>(?:(?!百度).)*?</a>");
            Matcher matcher = pattern.matcher(str);
            while(matcher.find()) {
                System.out.println(matcher.group());
            }          
        }
    }
      

  2.   

    谢谢楼上的。
    虽然还没有完全弄懂^_^,但是已经得到启示了(在看非捕获组)。
    我在我的博客文章http://blog.csdn.net/zhuche110/archive/2008/04/01/2238150.aspx
    引用了楼上的代码,同时加紧了自己的理解。
    我自己后来用了另外的思路,可能没那么简洁 但是对我自己的工作可能会思路清晰点^_^。
    不过 楼上的是基本功~