C#用正则表达式 获取标签的属性或值
整理两个 在C#中,用正则表达式 获取网页源代码标签的属性或值的方法 :
1、获取标签中的值: string str="<a href=\"www.csdn.net\" 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=\"www.csdn.net\" class=\"main\">CSDN</a>" 获取 “href” 的结果:www.csdn.net
调用例子: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> 之间的内容,<div class="brand_items"> 跟 </div> 可以重复出现
比如 :
| <div class="cont_left"> | |
| <div class="cont_left_items"><a href="left.php?cid=QDE=" class="ma">数码手机</a></div> | |
| <div class="cont_left_items"><a href="left.php?cid=QTE=" class="ma">整机电教</a></div> | |
| <div class="cont_left_items"><a href="left.php?cid=QjE=" class="ma">办公耗材</a></div> | |
| <div class="cont_left_items2"><a href="left.php?cid=RDE=" class="ma2">硬件周边</a></div> | |
| <div class="cont_left_items"><a href="left.php?cid=RTE=" class="ma">网络通讯</a></div> | |
| <div class="cont_left_items"><a href="left.php?cid=RzE=" class="ma">安防监控</a></div> | |
| <div class="cont_left_items"><a href="left.php?cid=QDFAMQ==" class="ma">IT综合</a></div> | |
| </div> | |
| <div class="cont_right"> | |
| <div class="cont_right_items2"><a href="prod_list.php?cid=RDE=&sid=SDE=" class="ma3" target="_parent">摄像头</a></div> | |
| <div class="rg_brand_list"> <div class="brand_items"><a href="prod_list.php?bid=QDFBMUYx&cid=RDE=&sid=SDE=" target="_parent" class="ma2">罗技</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=QDFCMUQx&cid=RDE=&sid=SDE=" target="_parent" class="ma2">新贵</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=QDFDMUYx&cid=RDE=&sid=SDE=" target="_parent" class="ma2">清华紫光</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=QDFDMUUx&cid=RDE=&sid=SDE=" target="_parent" class="ma2">达克浩思</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=QDFGMUYxPzFBMQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">ASJ奥视嘉</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=RzE/MUUxRDFHMQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">百视通</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=RzE/MUgxSDE/MQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">创新</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=RzFAMT8xPzE/MQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">海畅</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=RzFAMT8xQDFEMQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">昂达</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=RzFAMT8xQjFAMQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">网眼</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=RzFBMUMxPzFAMQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">网缘</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=RzFDMUUxQjFGMQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">黑石</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=QDFBMUgx&cid=RDE=&sid=SDE=" target="_parent" class="ma2">奥尼ANC</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=QDFCMUYx&cid=RDE=&sid=SDE=" target="_parent" class="ma2">鼎易</a></div> | |
| <div class="brand_items"><a href="prod_list.php?bid=RzFCMUcxQDE/MQ==&cid=RDE=&sid=SDE=" target="_parent" class="ma2">清华同方</a></div> | |
| </div> <div class="cont_right_items"><a href="prod_list.php?cid=RDE=&sid=QzFBMQ==" class="ma3" target="_parent">液晶显示器</a></div> | |
| <div class="cont_right_items"><a href="prod_list.php?cid=RDE=&sid=RDE/MQ==" class="ma3" target="_parent">主板</a></div> | |
| <div class="cont_right_items"><a href="prod_list.php?cid=RDE=&sid=RDFAMQ==" class="ma3" target="_parent">显卡</a></div> | |
| <div class="cont_right_items"><a href="prod_list.php?cid=RDE=&sid=RDFBMQ==" class="ma3" target="_parent">CPU</a></div> | |
| <div class="cont_right_items"><a href="prod_list.php?cid=RDE=&sid=RTFAMQ==" class="ma3" target="_parent">散热器</a></div> | |
| <div class="cont_right_items"><a href="prod_list.php?cid=RDE=&sid=RzFDMQ==" class="ma3" target="_parent">键盘</a></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();//每个<div class="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标签:string str1 = Regex.Replace(str, @"</?a[^>]*>", "");
清除所有script 包括script里面的代码 str1 = Regex.Replace(str, @"<script[^>]*>([\s\S](?!<script))*?</script>", "");
C#用正则表达式 获取标签的属性或值的更多相关文章
- [转载]C#用正则表达式 获取网页源代码标签的属性或值
最近调试程序需要用到获取网页指定标签的属性和值,找到了一个比较好的正则匹配方法,特此备份. [原]C#用正则表达式 获取网页源代码标签的属性或值 整理两个 在C#中,用正则表达式 获取网页源代码标签的 ...
- js-取值&赋值-获取某标签某属性的值
js 取值&赋值-获取某标签某属性的值 CreateTime--2016年10月16日16:35:34 Author:Marydon 1.取值 //方法一 //自定义属性必须用getAtt ...
- Spring中bean标签的属性和值:
Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...
- HTML标签CSS属性默认值汇总
HTML标签CSS属性默认值,在你需要还原默认值的时候比较有用. 以前一直在找这份文档,今天偶然在网上看到了.除了inline和block的定义,主要是要注意body|h1~h6|blockquote ...
- [Jmeter]Xpath获取元素某个属性的值,以及获取最后一个元素某个属性的值
XPath获取元素某个属性的值 XPath query: clients/attribute::total XPath获取最后一个元素某个属性的值 XPath query: /clients/c ...
- C#用正则表达式 获取网页源代码标签的属性或值
1.有url获取到网页源代码: using System.Web; using System.IO; using System.Net; private void GetHtmlinfo(string ...
- 实例之跑马灯,函数创建、通过ID获取标签及内部的值,字符串的获取与拼接、定时器的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java反射获取Object的属性和值
在看反射顺便做个笔记,目前知道的反射的Object都是要有对象的也就是实体Bean. import java.lang.reflect.Field; import java.util.ArrayLis ...
- js(jQuery)获取自定义data属性的值
有时候因为需要在标签上设置自定义data属性值, <div class="col-sm-6 col-md-4" id="get_id" data-c_id ...
随机推荐
- 【英语】Bingo口语笔记(39) - Get系列
- Java之路
Java程序员 高级特性 反射.泛型.注释符.自动装箱和拆箱.枚举类.可变参数.可变返回类型.增强循环.静态导入 核心编程 IO.多线程.实体类.集合类.正则表达式.XML和属性文件 图形编程 AWT ...
- ASIHTTPRequest详解
ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPRe ...
- 一天一点MySQL复习——获取数据库系统时间、变量赋值、变量比较
一.SQL获取系统时间 mysql> select now() from dual; +---------------------+ | now() | +------------------- ...
- loadrunner中lr_log_message和lr_output_message 的区别
LoadRunner中lr_output_message和lr_log_message(1)在vgen中,我们必须写输出函数输出信息,将我们所想要了解的信息用函数输出,主要有这么几个函数输出信息: l ...
- android命名规范
Android 开发规范 (陈杨) (一)注意事项 1. 编码方式统一用UTF-8. Android Studio默认已是UTF-8,只要不去改动它就可以了. 2. 缩进统一为4个空格,将Tab si ...
- 使用Ajax在javascript中调用后台C#函数
使用Ajax在javascript中调用后台C#函数 最近一段时间在紧跟一个网站的项目,数据库中用户表的UserName要求是唯一的,所以当用户选定一个用户名进行注册时要首先检查该用户名是否已被占用, ...
- 约瑟夫环 --- 面向对象 --- java代码
约瑟夫环 的 面向对象 解法 罗马人占领乔塔帕特后,39个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个 ...
- Unreal Engine 虚幻引擎宣布对开发者免费
虚幻引擎4现在可供每个人免费使用,而且所有未来的更新都将免费!您可以下载引擎并将其用于游戏开发的各个方面,包括教育.建筑以及可视化,甚至虚拟现 实.电影和动画. 当您发布游戏或应用时,在您的每个游戏在 ...
- Epic - Decimal Number
Let the user enter a decimal number. Therange allowed is 0.0001 to 0.9999. Only four decimal places ...