想要正则表达式以下?

时间:2012-05-26 07:59:29

标签: android html regex

我有:

<description><![CDATA[<div><b>Details:</b> <div class=ExternalClassCDAAC64F989B48B1AE79489DFBF8C27C><div><span style="font-size:8pt;font-weight:700"><font color="#008080" face=Verdana><span style="text-decoration:none"><a style="text-decoration:none" href="http://unipune.ac.in/other_academic_and_service_units/board_students_welfare/pdf/Annual_Essay_Competition_Covering_26-5-12.pdf" target="_blank"><font color="#008080">Letter Regarding Annual Essay Prize Competition</font></a></span></font></span></div></div></div>
<div><b>Expires:</b> 8/14/2012</div>
]]></description>

我需要一个正则表达式,它只给我href内容。我试过这个:

String link1 = a.substring(a.indexOf("href=\""), a.indexOf("\""));

但它给了我一个强制关闭错误。

我想要的输出有点像这样:

link = http://unipune.ac.in/other_academic_and_service_units/board_students_welfare/pdf/Annual_Essay_Competition_Covering_26-5-12.pdf

有人能帮助我吗?

3 个答案:

答案 0 :(得分:1)

试试这个::::但只有这个原始字符串的格式相同。

String[] separated = a.spilt("href=\"");
String[] first = separated[1].spilt("\" target");
String link1 = first[0];

答案 1 :(得分:0)

试试这个

\bhref="([^"<>]+)"

<强>代码

try {
    String resultString = subjectString.replaceAll("\\bhref=\"([^\"<>]+)\"", "link=$1");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
} catch (IllegalArgumentException ex) {
    // Syntax error in the replacement text (unescaped $ signs?)
} catch (IndexOutOfBoundsException ex) {
    // Non-existent backreference used the replacement text
}

测试here

希望这有帮助。

答案 2 :(得分:-1)

试试这个。

String link1 = a.substring(a.indexOf("href=\""), a.indexOf("target=")-1);