<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_29">
第一行:一些内容<br />
第二行:一些内容<br />
</td></tr></table>
求一个正则表达式。要求:得到里面的文字,这些文字作为一个整体,而不是通过单行匹配得到的。
例如这样"(.*<br />)",就不符合我的要求。

解决方案 »

  1.   

    换个思路的话,你就是要去除所有的 <> 内容是吧?String text = str.replaceAll("<[^>]+>", "");
      

  2.   

    恩,我的确是想要去除<>内容。但是网页源代码会有好多上面的代码片段,所以我想先匹配上面的内容作为一个整体,然后在用你的正则得到内容。
      

  3.   

    代码片段也可以直接用正则先消灭掉:
    str = str.replaceAll("<script[^>]*>.*</script>", "");
      

  4.   


    public static void main(String[] args) {
    String testStr = "<html><head></head><body><table cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"t_f\" id=\"postmessage_29\">\n 第一行:一些内容<br />\n 第二行:一些内容<br />\n </td></tr></table></body></html>";
    String tableReg = "<table.*?><tr><td class=\"[\\w_]+\" id=\"[\\w_]+\">*.*?</table>";
    String contextReg = ">[\r\n\\s]*([^<]+)<br";
    Pattern p = Pattern.compile(tableReg, Pattern.DOTALL);
    Matcher matcher = p.matcher(testStr);
    while (matcher.find()) {
    String tablePattern = matcher.group(0);
    System.out.println("全体:" + tablePattern);
    System.out.println("-------------------------------------------------");
    Pattern contextPattern = Pattern.compile(contextReg);
    Matcher contextMatcher = contextPattern.matcher(testStr);
    while (contextMatcher.find()) {
    System.out.println(contextMatcher.group(1));
    }
    }
    }
      

  5.   

    //先剔除body以外的script,style。 然后再剔除标签
    String text = str.replaceAll(".*<body[^>]*>(.*)</body>.*","$1").replaceAll("<[^>]+>", "");
      

  6.   

    String text = str.replaceAll("<[^>]+>", "");