String strTemp="<span>1</span><img src='images/1.gif'><a href='images/1.gif'>link1</a><a href='www.sina.com'>link2</a>";
String partn="(<a\\s+href\\s*=(\\s*(\"[^\"]*\"|[^\\s>])[^>]*)>([^<]*))(.*)(</a>)";
Vector colInoput= new Vector();
Pattern pattern=Pattern.compile(partn);
Matcher matcher=pattern.matcher(strTemp);
while(matcher.find()){
     colInoput.add(matcher.group(1));
}
for(int i=0;i<colInoput.size();i++){
     out.println(colInoput.elementAt(i).toString()+"<hr>");
}
//////////////////////////////////
问题:我现在想把strTemp中的<a>……</a>全部取出来该怎么做?
谢谢先!

解决方案 »

  1.   

    while(matcher.find()){
         colInoput.add(matcher.group(3)+matcher.group(5));
    }
      

  2.   

    我帮你作了整理,祝你好运!
    public static void testRegex()
    throws Exception
    {
    String content = "<span>1</span><img src='images/1.gif'><a href='images/1.gif'>link1</a><br><a href='www.sina.com'>link2</a>";
    String value = null;String value2 = null;
         Pattern dtPattern = Pattern.compile("<a\\s.*?>(.*?)</a>",
         Pattern.CASE_INSENSITIVE);
         Matcher dtMatcher = dtPattern.matcher(content);
         Vector anchorVector= new Vector();
         while (dtMatcher.find())
         {
         value = dtMatcher.group(0);
         value = value.trim();
         value2 = dtMatcher.group(1);
         value2 = value2.trim();
        
         anchorVector.add(value2);
        
         System.out.println( "raw text:" + value + "-->anchor text:" + value2 );
        
         content = content.replaceAll(value, "");
         dtMatcher = dtPattern.matcher(content);
         }
    }
      

  3.   

    我帮你作了整理,祝你好运!
    public static void testRegex()
    throws Exception
    {
    String content = "<span>1</span><img src='images/1.gif'><a href='images/1.gif'>link1</a><br><a href='www.sina.com'>link2</a>";
    String value = null;String value2 = null;
         Pattern dtPattern = Pattern.compile("<a\\s.*?>(.*?)</a>",
         Pattern.CASE_INSENSITIVE);
         Matcher dtMatcher = dtPattern.matcher(content);
         Vector anchorVector= new Vector();
         while (dtMatcher.find())
         {
         value = dtMatcher.group(0);
         value = value.trim();
         value2 = dtMatcher.group(1);
         value2 = value2.trim();
        
         anchorVector.add(value2);
        
         System.out.println( "raw text:" + value + "-->anchor text:" + value2 );
        
         content = content.replaceAll(value, "");
         dtMatcher = dtPattern.matcher(content);
         }
    }
      

  4.   

    String strTemp="<span>1</span><img src='images/1.gif'><a href='images/1.gif'>link1</a>" + 
                    "<a href='www.sina.com'>link2</a>";
    //其实你就把下面的copy到你那里,别的都不用该。
    String partn="(<a\\s+href\\s*=(\\s*(\"[^\"]*\"|[^\\s>])[^>]*)>([^<]*)(.*)(</a>))";
    Vector colInoput= new Vector();
    Pattern pattern=Pattern.compile(partn);
    Matcher matcher=pattern.matcher(strTemp);
    while(matcher.find()){
         colInoput.add(matcher.group(1));
    //colInoput.add(matcher.group());
    }
    for(int i=0;i<colInoput.size();i++){
         System.out.println(colInoput.elementAt(i).toString()+"<hr>");
    }
    }