1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C#用正则表达式 获取标签的属性或值

C#用正则表达式 获取标签的属性或值

时间:2019-08-30 09:37:32

相关推荐

C#用正则表达式 获取标签的属性或值

整理两个 在C#中,用正则表达式 获取网页源代码标签的属性或值的方法 :

1、获取标签中的值: string str="<a href=\"\" class=\"main\" >CSDN</a>" 结果:CSDN

调用例子:string name=GetTitleContent(str,"a");

/// <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); //获取<title>之间内容

Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);

string result = TitleMatch.Groups["Text"].Value;

return result;

}

2、获取标签中的属性: string str="<a href=\"\" class=\"main\">CSDN</a>" 获取 “href” 的结果:

调用例子:string href=GetTitleContent(str,"a","href");

/// <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;

}

注:以上方法为获取字符串中第一个结果的值。可以使用Foreach读取TitileMath中所有的匹配属性或值。

3。获取<div class="brand_items"> 跟 </div> 之间的内容,<divclass="brand_items"> 跟 </div> 可以重复出现

比如 :

Regex regex = new Regex("(?<=(<div class=\"brand_items\">))[.\\s\\S]*?(?=(</div>))", RegexOptions.IgnoreCase);

for (Match match = regex.Match(content); match.Success; match = match.NextMatch())

{

string d=match.Groups[0].ToString();//每个<divclass="brand_items"> </div>里的内容

}

获取<div class="info_mid_left"> 跟<div class="endarea"> 之间的内容,唯一性 <div class="info_mid_left"> 跟<div class="endarea"> 不重复出现

Regex regex1 = new Regex("(?<=(<div class=\"info_mid_left\">))[.\\s\\S]*?(?=(<div class=\"endarea\">))", RegexOptions.IgnoreCase);

string Pcontent = regex1.Match(content).Groups[0].Value;

获取<td class="paramFontS2" style="text-align:center; font-size:13px;">技嘉 H61M-DS2DVI </td> td里的Text内容,td有多种不确定属性的时候

Regex regex2 = new Regex("<td[^>]*?>(?<Text>[^<]*)</td>", RegexOptions.IgnoreCase);

for (Match match2 = regex2.Match(d1); match2.Success; match2 = match2.NextMatch())

{

name = match2.Groups["Text"].Value;

}

获取input控件的value值

public string FindValueByName(string str, string inputname)

{

//string reg = @"<input name=""(?<name>.*?)"" [\s\S]*?value=""(?<value>.*?)"" [\s\S]*?>";

string reg = "<input[^>]+name=\"*(?<name>[^\\s\">]+)\"*[^>]*value=\"*(?<value>[^\\s\">]+)\"*[^>]*>";

Regex r = new Regex(reg, RegexOptions.None);

Match match = r.Match(str);

string aa = "";

while (match.Success)

{

string name = match.Groups["name"].ToString();

string value = match.Groups["value"].ToString();

if (name == inputname)

{

return value;

}

else

{

match = match.NextMatch();

}

}

return aa;

}

清除所有a标签:stringstr1 = Regex.Replace(str, @"</?a[^>]*>", "");

清除所有script 包括script里面的代码 str1 = Regex.Replace(str, @"<script[^>]*>([\s\S](?!<script))*?</script>", "");

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。