<td width="82">2011-04-05</td>
<td width="45">20:37</td>
<td align="left" width="339">
<a href="#" onmouseout="hideTip()" onmouseover="showTip('帮忙@0',event)">
<font color=#FF0000></font>
</a>
<a href="#" onmouseout="hideTip()" onmouseover="showTip('呵呵',event)">
<font color=#0000FF> 张三 </font>
</a>
哈哈,
<b>大笑了</b>
</td>以上是一个String  现在我需要用到String.replaceAll()方法,把标签等不要的内容全部替换掉,剩下所需的东西..
"2011-04-05 20:37 张三哈哈,大笑了"  这是取到最后的内容主要用到就是一个正则表达式..

解决方案 »

  1.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class test2 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String str = "<td width=\"82\">2011-04-05</td><td width=\"45\">20:37</td><font color=#0000FF> 张三 </font></a>哈哈,<b>大笑了</b>";
    String regex =  "(?<=>)[^<]+?(?=<)";
    Matcher m = Pattern.compile(regex).matcher(str);
    while(m.find()){
    System.out.println(m.group());
    }
    }}have a try
      

  2.   

    public class FilterHtmlTags { public static String delHtml(String inputString) {
    String htmlStr = inputString;
    String textStr = "";
    java.util.regex.Pattern p_html;
    java.util.regex.Matcher m_html;
    try {
    String regEx_html = "<[^>]+>";
    p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
    m_html = p_html.matcher(htmlStr);
    htmlStr = m_html.replaceAll(""); textStr = htmlStr; } catch (Exception e) {
    System.err.println("Html2Text: " + e.getMessage());
    } return textStr;
    } public static void main(String[] args) {
    String html = "<td width=\"82\">2011-04-05</td><td width=\"45\">20:37</td>"
    + "<td align=\"left\" width=\"339\">"
    + "<a href=\"#\" onmouseout=\"hideTip()\" onmouseover=\"showTip('帮忙@0',event)\">"
    + "<font color=#FF0000></font></a>"
    + "<a href=\"#\" onmouseout=\"hideTip()\" onmouseover=\"showTip('呵呵',event)\">"
    + "<font color=#0000FF> 张三 </font></a>哈哈,<b>大笑了</b></td>";
    System.out.println(delHtml(html));
    }
    }