嘿,我一直在努力使用这个正则表达式而且我没有想法.我有这种类型的字符串(不是所有这些都在这里,但只有这两种类型),我必须提取th标签之间的部分.
<th class="tip" title='manje'>manje</th>
<th class="tip" title='ne d.'>ne d.</th>
<th class="tip" title='manje'>manje</th>
<th class="tip" title='to?no'>to?no</th>
<th class="tip" title='više'>više</th>
<th class="tip" title='m./t.'>m./t.</th>
<th class="tip" title='v./t.'>v./t.</th>
<th class="tip">daje</th>
<th class="tip">X2</th>
<th class="tip">12</th>
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些组合,如果第th个标签中没有该属性"title",我只得到该值.
如果th标签中没有"title"属性,则此模式仅提取内容:
Pattern pattern = Pattern.compile("<th class=\"tip\"[\\s*|[.]{0,20}]>(.*?)\\s*</th>");
Run Code Online (Sandbox Code Playgroud)
这一个还:
Pattern patternType = Pattern.compile("<th class=\"tip\"[\\s*|[.]{0,20}]>(.*?)\\s*</th>");
Run Code Online (Sandbox Code Playgroud)
有什么建议?TNX
正则表达式并不适用于所有情况.改用Jsoup:
package so6235727;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class PrintContent {
private static final String html = //
"<th class=\"tip\" title='manje'>manje</th>\r\n" + //
"<th class=\"tip\" title='ne d.'>ne d.</th>\r\n" + //
"<th class=\"tip\" title='manje'>manje</th>\r\n" + //
"<th class=\"tip\" title='to?no'>to?no</th>\r\n" + //
"<th class=\"tip\" title='više'>više</th>\r\n" + //
"<th class=\"tip\" title='m./t.'>m./t.</th>\r\n" + //
"<th class=\"tip\" title='v./t.'>v./t.</th>\r\n" + //
"<th class=\"tip\">daje</th>\r\n" + //
"<th class=\"tip\">X2</th>\r\n" + //
"<th class=\"tip\">12</th>\r\n";
public static void main(String[] args) {
Document jsoup = Jsoup.parse(html);
Elements headings = jsoup.select("th.tip");
for (Element element : headings) {
System.out.println(element.text());
}
}
}
Run Code Online (Sandbox Code Playgroud)
看看这有多容易?