使用Java和正则表达式帮助从html标记中提取文本

使用Java和正则表达式帮助从html标记中提取文本,java,html,regex,tags,Java,Html,Regex,Tags,我想使用正则表达式从html文件中提取一些文本。我正在学习正则表达式,但我仍然很难理解它。我有一个代码,它提取了和之间包含的所有文本,如下所示: public class Harn2 { public static void main(String[] args) throws IOException{ String toMatch=readFile(); //Pattern pattern=Pattern.compile(".*?<body.*?>(.*?)</body&

我想使用正则表达式从html文件中提取一些文本。我正在学习正则表达式,但我仍然很难理解它。我有一个代码,它提取了
之间包含的所有文本,如下所示:

public class Harn2 {

public static void main(String[] args) throws IOException{

String toMatch=readFile();
//Pattern pattern=Pattern.compile(".*?<body.*?>(.*?)</body>.*?"); this one works fine
Pattern pattern=Pattern.compile(".*?<table class=\"claroTable\".*?>(.*?)</table>.*?"); //I want this one to work
Matcher matcher=pattern.matcher(toMatch);

if(matcher.matches()) {
    System.out.println(matcher.group(1));
}

}

 private static String readFile() {

      try{
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("user.html");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine = null;
            //Read File Line By Line
            while (br.readLine() != null)   {
                // Print the content on the console
                //System.out.println (strLine);
                strLine+=br.readLine();
            }
            //Close the input stream
            in.close();
            return strLine;
            }catch (Exception e){//Catch exception if any

                System.err.println("Error: " + e.getMessage());
                return "";
            }
}
}
公共类Harn2{
公共静态void main(字符串[]args)引发IOException{
字符串toMatch=readFile();
//Pattern=Pattern.compile(“.*?(.*?。”);这个很好用
Pattern=Pattern.compile(“.*?(.*?.*?);//我希望这个能正常工作
Matcher Matcher=pattern.Matcher(toMatch);
if(matcher.matches()){
系统输出println(匹配器组(1));
}
}
私有静态字符串readFile(){
试一试{
//打开第一个文件
//命令行参数
FileInputStream fstream=新的FileInputStream(“user.html”);
//获取DataInputStream的对象
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
字符串strLine=null;
//逐行读取文件
while(br.readLine()!=null){
//在控制台上打印内容
//System.out.println(strLine);
strLine+=br.readLine();
}
//关闭输入流
in.close();
返回斯特林;
}catch(异常e){//catch异常(如果有)
System.err.println(“错误:+e.getMessage());
返回“”;
}
}
}
它可以像这样工作,但现在我想提取标签之间的文本:

因此,我将正则表达式字符串替换为
“*?(.*?).*?”
我也试过了
“*?(*?)*?”
但它不起作用,我不明白为什么。html文件中只有一个表,但javascript代码中出现了“table”:“…dataTables.js…”这可能是错误的原因吗

提前谢谢你帮助我

编辑:extranct的html文本类似于:

<body>
.....
<table class="claroTable">
<td><th>some data and manya many tags </td>
.....
</table>

.....
一些数据和许多标签
.....

我想提取的是介于
之间的任何内容。如上所述,这是一个不适合使用正则表达式的地方。只有在你真正需要的时候才使用正则表达式,所以如果可以的话,基本上尽量远离它。请看这篇关于解析器的文章:


以下是如何使用:

是的,你也可以用正则表达式来实现它,但它永远不会这么容易

更新:正则表达式模式的主要问题是缺少以下标志:

Pattern-Pattern=Pattern.compile(“.*?(.*?).*?”,Pattern.DOTALL);
如果您只希望指定的表标记包含内容,可以执行以下操作:

String tableTag = 
    Pattern.compile(".*?<table.*?claroTable.*?>(.*?)</table>.*?",Pattern.DOTALL)
           .matcher(html)
           .replaceFirst("$1");
String tableTag=
Pattern.compile(“.*?(.*?*”,Pattern.DOTALL)
.matcher(html)
.第一次更换(“1美元”);

(更新:现在只返回表标记的内容,而不是表标记本身)

如果要从html提取数据:请使用html解析器。如果您想学习RegExp:不要使用html或xml输入。迟早你会意识到,regexp'ing html不起作用。@nimchinpsky我感觉有人会发布这个lol。@Matt它被浏览了293307次,incredible@NimChimpsky当前位置但似乎没有人注意到。收件人:安德烈亚斯和马特:我知道,但我必须使用它。这里的要点是使用正则表达式,我没有选择。编程语言并不重要,但使用正则表达式是一项要求,因此我真的希望得到一些帮助。thks@vallllll好的,我已经更新了我的答案,以实际解决您的正则表达式问题。thks肖恩·帕特里克·弗洛伊德,它实际上与body标记一起工作,但我想提取table标记,但该标记不起作用:。。。。要提取的数据。。。因此,类似Pattern=Pattern.compile(“.*?(.*?).*?”)的代码会返回Sean Patrick Floyd,但它会再次返回整个html字符串,就好像什么都没发生一样。我不明白replaceFirst(…)做什么???@vallllll replaceFirst($1)表示用第一个匹配的第一个匹配组替换字符串。我刚看到你想要表格标签之间的内容,会相应地更新我的答案。@valllll那么你做错了什么。以下是我的代码的工作版本:
Pattern pattern=Pattern.compile(".*?<body.*?>(.*?)</body>.*?",Pattern.DOTALL);
String tableTag = 
    Pattern.compile(".*?<table.*?claroTable.*?>(.*?)</table>.*?",Pattern.DOTALL)
           .matcher(html)
           .replaceFirst("$1");