0

How to extract these messages by regex or jsoup ? 19040172b-1SQL Server Developzheng3-5,7-14D-101

<div id="AE9D7F630640426F8457A661607D2B8E-5-2" style="display: none;" class="kbcontent">
  19040172b-1
  <br>SQL Server Develop
  <br>
  <font title="teacher">zheng</font>
  <br>
  <font title="week">3-5,7-14</font>
  <br>
  <font title="classroom">D-101</font>
  <br>
 </div> 

I have tried the following ways but failed.

1. Pattern pattern = Pattern.compile(">(.*?)<br>");

2. Elements msg = doc.select(":matchesOwn([>.*?<br>])");
Guo Eason
  • 3
  • 5

2 Answers2

1

1) First, it's never a good idea to parse HTMl with a regex. You can read more about that here.

2)You can just take all text between tags.

Document doc = Jsoup.parse(file, charsetName);
String text= doc.text();
System.out.println(text);
Community
  • 1
  • 1
dakatamen
  • 11
  • 3
0
String html = "<div id=\"AE9D7F630640426F8457A661607D2B8E-5-2\" style=\"display: none;\" class=\"kbcontent\">  19040172b-1  <br>SQL Server Develop  <br>  <font title=\"teacher\">zheng</font>  <br>  <font title=\"week\">3-5,7-14</font>  <br>  <font title=\"classroom\">D-101</font>  <br> </div> ";
html = html.replaceAll("<br>", "#~#");
Document doc = Jsoup.parse(html.toString());
String newHtml = doc.text();
String[] ary = newHtml.split("#~#");

This will do the job, yet there may be other clean ways to replace the br tag.

bilgec
  • 171
  • 7