【问题标题】:Extracting text in html using Java Regex使用 Java 正则表达式提取 html 中的文本
【发布时间】:2016-09-04 22:23:21
【问题描述】:

我需要从 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);
    }
}

}

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>

mat.group(1) 被打印为“null”而不是文本。非常感谢任何帮助。

【问题讨论】:

    标签: java html regex


    【解决方案1】:

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

        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);      
    }    
    

    }

    【讨论】:

    • 根据您的建议取得了一些进展。但我仍然在输出中得到唯一的“供应”一词。我想获取所有字体标签的文本。
    • 我也在输出中得到这些 >
    • 我做了一些改进并编辑了我的最后一个答案。试试它是否适合你。但是我还是推荐使用像jsoup这样的html解析器,这样解析html就很容易了
    猜你喜欢
    • 2010-09-15
    • 1970-01-01
    • 2011-12-30
    • 2023-03-18
    • 2018-05-08
    • 2017-10-31
    • 2021-12-12
    相关资源
    最近更新 更多