使用Java正则表达式提取html中的文本

使用Java正则表达式提取html中的文本,java,html,regex,Java,Html,Regex,我需要从html标签中提取文本。我已经写了一个代码,但文本没有被提取。下面是我的代码 import java.util.regex.Matcher; import java.io.BufferedReader; import java.io.FileReader; import java.util.regex.Pattern; class getFontTagText{ String result = null; public static void main(String args[]){

我需要从html标签中提取文本。我已经写了一个代码,但文本没有被提取。下面是我的代码

import java.util.regex.Matcher;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Pattern;
class getFontTagText{
String result = null;
public static void main(String args[]){
    try{
           getFontTagText text = new getFontTagText();
           BufferedReader r = new BufferedReader(new FileReader("target.html"));
           Pattern p = Pattern.compile("<FONT FACE=\"Arial\" SIZE=\"1\" COLOR=\"\\W|_000000\" LETTERSPACING=\"0\" KERNING=\"0\">(//AZUZZU Full Service Provision)</FONT>",Pattern.MULTILINE);
           String line;
           System.out.println("Came here");
           while((line = r.readLine()) != null){
           Matcher mat = p.matcher(line);

           while(mat.find()){
                System.out.println("Came here");
                String st = mat.group(1);
                System.out.format("'%s'\n", st);
            }
        }
    }catch (Exception e){
        System.out.println(e);
    }
}
import java.util.regex.Matcher;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.util.regex.Pattern;
类getFontTagText{
字符串结果=null;
公共静态void main(字符串参数[]){
试一试{
getFontTagText=新的getFontTagText();
BufferedReader r=新的BufferedReader(新的文件阅读器(“target.html”);
Pattern p=Pattern.compile((//azuzu完整服务提供)”,Pattern.MULTILINE);
弦线;
System.out.println(“来到这里”);
而((line=r.readLine())!=null){
Matcher mat=p.Matcher(线);
while(mat.find()){
System.out.println(“来到这里”);
字符串st=材料组(1);
System.out.format(“%s”\n”,st);
}
}
}捕获(例外e){
系统输出打印ln(e);
}
}
}

html文件在这里

     <P ALIGN="LEFT">
         <FONT FACE="Arial" SIZE="1" COLOR="#000000" LETTERSPACING="0" KERNING="0">ZUZZU Full Service Provision</FONT>
     </P>
     <P ALIGN="LEFT">
         <FONT FACE="Arial" SIZE="1" COLOR="#000000" LETTERSPACING="0" KERNING="0">&uuml; &ouml; &auml; &Auml; &Uuml; &Ouml; &szlig;</FONT>
     </P>

ZUZZU提供全面服务

üöäÄÜÖ&斯兹利格;


物料组(1)打印为“空”,而不是文本。非常感谢您的帮助。

我建议您使用jsoup。jsoup是一个Java库,用于使用CSS和类似jquery的方法提取和操作HTML数据。在您的情况下,它可能看起来像这样:

    public static void jsoup() throws IOException{
    File input = new File("C:\\users\\uzochi\\desktop\\html.html");
    Document doc = Jsoup.parse(input, "UTF-8");
    Elements es = doc.select("FONT");//select tag 
    for(Element e : es){
        System.out.println(e.text());
    }    
}
例如,如果您喜欢使用正则表达式,只需匹配>和<之间的文本即可

public static void regex(){
Pattern pat = Pattern.compile("<FONT [^>]*>(.*?)</FONT>");//
String s = "<html>\n" +
            "<body>\n" +
            "\n" +
            "<P ALIGN=\"LEFT\">\n" +
            "         <FONT FACE=\"Arial\" SIZE=\"1\" COLOR=\"#000000\" LETTERSPACING=\"0\" KERNING=\"0\">ZUZZU Full Service Provision</FONT>\n" +
            "     </P>\n" +
            "     <P ALIGN=\"LEFT\">\n" +
            "         <FONT FACE=\"Arial\" SIZE=\"1\" COLOR=\"#000000\" LETTERSPACING=\"0\" KERNING=\"0\">&uuml; &ouml; &auml; &Auml; &Uuml; &Ouml; &szlig;</FONT>\n" +
            "     </P>\n" +
            "\n" +
            "</body>\n" +
            "</html>";
Matcher m = pat.matcher(s);
while (m.find()) {
    String found = m.group(1);
    System.out.println("Found : " + found);      
}    
公共静态void regex(){
Pattern pat=Pattern.compile(“]*>(.*)”//
字符串s=“\n”+
“\n”+
“\n”+
“

\n”+ “ZUZZU完全服务提供\n”+ “

\n”+ “

\n”+ “ü;ö;ä;ä;ü;ö;ß;\n”+ “

\n”+ “\n”+ “\n”+ ""; 匹配器m=匹配器; while(m.find()){ 找到的字符串=m.group(1); System.out.println(“Found:+Found”); }

}

根据你的建议取得了一些进展。但我还是得到了输出中唯一的一个词“Provision”。我想获取所有字体标记的文本。我还将在输出中获取这些><字符。我想把这些也删掉。我做了一些改进并编辑了我最后的答案。如果对你有用,试试看。但我仍然建议使用像jsoup这样的html解析器,这将使解析html变得非常简单