抓取了一個網頁,現在要截取字段
" <--開始--> 內容 <--結束--> "
public string HttpString_Filtrate(string Http_String, string Begin_String, string End_String)
{
int Begin_Int = Http_String.IndexOf(Begin_String);
int End_Int = Http_String.IndexOf(End_String);
string ReString = Http_String.Substring(End_Int,Begin_Int);
return ReString;
}
現在要得到內容,我寫這個好象不行。。。。。 <--開始--> 和 <--結束--> 爲不固定值
解決方案一:
符號保留啊
比如: " <hr> 其它 <--開始--> 內容 <br> 內容 <--結束--> 其它 <hr> "
得到 "內容 <br> 內容 "
解決方案二:寫匹配正則表達式
//刪除所有的html標記
public static string delHtml(string str)
{
if (str != null && str.Trim() != " ")
return Regex.Replace(str, " <[^> ]+> ", " ");
return str;
}
// 刪除字符串中的特定標記
//isContent:是否清除內容
public static string delTag(string str, string tag, bool isContent)
{
if (tag == null || tag == " ")
{
return str;
}
if (isContent) //要求清除內容
{
return Regex.Replace(str, string.Format( " <({0})[^> ]*> ([//s//S]*?)
</////1> ", tag), " ", RegexOptions.IgnoreCase);
}
return Regex.Replace(str, string.Format(@ "( <{0}[^> ]*(> )?)|( </{0}[^> ]
*> )| ", tag), " ", RegexOptions.IgnoreCase);
}
// 刪除字符串中的一組標記
//isContent:是否清除內容
public static string delTagArray(string str, string tagA, bool isContent)
{
string[] tagAa = tagA.Split( ', ');
foreach (string sr1 in tagAa) //遍歷所有標記,刪除
{
str = delTag(str, sr1, isContent);
}
return str;
}
解決方案三:
如果End_String是:
<table width=90% border=0 align=center>
<tr> </tr>
</table>
怎麼辦?
用SubString個人覺得效率極低,用正則好!有其他方案歡迎指教一二