正则表达式获取html标签间的内容

发布日期:2018-02-08    浏览次数:823
/// <summary>  
/// 获取字符中指定标签的值  
/// </summary>  
/// <param name="str">字符串</param>  
/// <param name="title">标签</param>  
/// <param name="attrib">属性名</param>  
/// <returns>属性</returns>  
public static string GetTitleContent(string str, string title, string attrib)
{
string tmpStr = string.Format("<{0}[^>]*?{1}=(['""]?)(?<url>[^'""\\s>]+)\\1[^>]*>", title, attrib);   
Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);
string result = TitleMatch.Groups["url"].Value;
return result;
}


/// <summary>
/// 获取字符中指定标签的值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="title">标签</param>
/// <returns>值</returns>
public static string GetTitleContent(string str, string title)
{
string tmpStr = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", title, title);
Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);
string result = TitleMatch.Groups["Text"].Value;
return result;
}
 

本文网址:https://www.wyxxw.cn/blog-detail-2-6-98.html

返回列表

非特殊说明,本文版权归原作者所有,转载请注明出处

提示:本站所有资源仅供学习与参考,请勿用于商业用途。图片来自互联网~如侵犯您的权益,请联系QQ:1067507709.

提示:转载请注明来自:https://www.wyxxw.cn/blog-detail-2-6-98.html 。 本文发布者:momo