我試圖提取標題元素內的文本並忽略其他所有內容。無法找出正則表達式問題
我已經看了這些文章,但他們似乎並沒有幫助:\
Regular expression to extract text between square brackets
String Pattern Matching In Java
Java Regex to get the text from HTML anchor (<a>...</a>) tags
主要的問題是我無法理解的應答者說的話同時試圖破解我自己的代碼。
以下是我通過閱讀Pattern article中的Java API進行管理的內容。
<title>(.*?)</title>
這是我的代碼來返回標題。
String title = null;
Matcher match = Pattern.compile("[<title>](.*?)[</title>]").matcher(this.webPage);
try{
title = match.group();
}
catch(IllegalStateException e)
{
e.printStackTrace();
}
我收到IllegalStateException異常,其中這樣說:
java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)
at java.util.regex.Matcher.group(Matcher.java:445)
at BrowserModal.getWebPageTitle(BrowserModal.java:21)
at BrowserTest.main(BrowserTest.java:7)
線21將是 「標題= match.group();」
請不要使用RegEx解析HTML。 [只需相信我們](http://stackoverflow.com/a/1732454)。改用HTML或XML解析器。 – 2013-02-28 05:26:20
馬特是對的。正則表達式不是正確的工具。舉一個例子來說明它有什麼問題,請考慮評論的可能性:'