开发者

Regular Expression to fetch the result between two tags in Java

开发者 https://www.devze.com 2022-12-27 18:34 出处:网络
I have an HTML page and I want to fetch the result between two tags <开发者_JAVA百科;b> and <BR>:

I have an HTML page and I want to fetch the result between two tags <开发者_JAVA百科;b> and <BR>:

<b>Defendants Name:</b>Donahue, Leah A                                  <BR>

What is the regular expression to fetch the words between these two tags?


I think this could work:

    String str = "<b>Defendants Name:</b>Donahue, Leah A                                                    <BR>";
    Pattern pattern = Pattern.compile(".*<b>(.*)<BR>.*", Pattern.UNIX_LINES);
    Matcher m = pattern.matcher(str);
    if (m.matches() == true)
    {
        System.out.println(m.group(1));
    }

And should print

"Defendants Name:Donahue, Leah A " (excluding the quotes).


You shouldn't use regexps for parsing HTML, use an HTML parser instead. Have a look at jsoup.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号