求正则表达式:怎么取得<a href="xxxxxx">和</a>之间的字符串?比如:<a href="xxxxxx">北京海淀区颐和园</a>,就取‘北京海淀区颐和园’,其它不要。xxxxxx为网址,可能带参数请各位老大给一个可用的算法,谢谢

解决方案 »

  1.   

    一定要用正则表达式提取吗?取 > 和 < 之间的内容啊。
    如果用正则表达式擦掉 < 和 > 这件的内容,那么剩下的也行啊。
    另外,构造一个xml,然后提取节点 a 的内容,也是可以的嘛。
      

  2.   

    public static void main(String[] args)
        {
            Untitled2 u = new Untitled2();
            String testString = "<a href=\"http://www.com.cn/a/b.jsp?name=value\">北京海淀区颐和园</a>";
            String r = testString.replaceAll("^<a.*\\\"\\s*>|</a>$","");
            System.out.println(r);
        }
      

  3.   

    这个链接最规则了,应该很好取的,匹配表达式如下
    <a\\s+href\\s*=(\\s*(\"[^\"]*\"|[^\\s>])[^>]*)>([^<]*)</a>
    想取什么自己去相应的组中取吧
      

  4.   

    public static void main(String[] args)
    {
    String reg="<[^>]*>([^<]*)<[^>]*>";
    Pattern pattern=Pattern.compile(reg);
    String s="<a href=\"xxxxxx\">北京海淀区颐和园</a>";
    Matcher matcher=pattern.matcher(s);
    while(matcher.find())
    {
    System.out.print(matcher.group(1));
    }
    }
      

  5.   

    通过抗干扰测试,好像还是这样好点:    public static void main(String[] args)
        {
            String testString = "<a href=\"http://www.com.cn/a/b.jsp?name=value\">><北京海淀区颐和园</a>";
            Pattern p = Pattern.compile("<[^>]*>(.*)</[^>]*>");
            Matcher m = p.matcher(testString);
            while (m.find())
            {
                System.out.println(m.group(1));
            }
        }
      

  6.   

    String testString = "<a href=\"http://www.com.cn/a/b.jsp?name=value\">北京海淀区颐和园</a>";
     String regExp2 ="(<a\\s+href\\s*=(\\s*(\"[^\"]*\"|[^\\s>])[^>]*)>([^<]*))(.*)(</a>)";
      r = testString.replaceAll(regExp2,"$4");
      System.out.println(r);