如果我們要解析一個(gè)html,想要得到界面里的某個(gè)標(biāo)簽值,那么用正則表達(dá)式比較方便。
比如,我們獲得了一個(gè)html界面,想獲得某個(gè)標(biāo)簽的值(這個(gè)標(biāo)簽有唯一屬性比如id=xxx,如果該類標(biāo)簽沒有唯一屬性的話,那么將會(huì)得到該類標(biāo)簽所有值),
代碼展示如下:
PRivate string GetValue(string data) { string returnStr = ""; MatchCollection mc = Regex.Matches(data, "<td id='cur_no2' .*?>.*?</td>"); //做一個(gè)要匹配的正則表達(dá)式, .*?表示所有的元素 foreach (Match m in mc) //遍歷匹配好的標(biāo)簽 { string str = m.Value.Replace("</td>", ""); //去掉標(biāo)簽后綴,用“”代替</td> str = Regex.Replace(str, "<.*?>", ""); // 去掉標(biāo)簽前綴,用“”代替<.*?> returnStr += str; //如果匹配多個(gè)標(biāo)簽,那么標(biāo)簽里面的值拼接 } return returnStr; }
新聞熱點(diǎn)
疑難解答
圖片精選