正则的那些事,备忘...
来源:互联网 发布:穆雅斓的淘宝店怎么了 编辑:程序博客网 时间:2024/05/24 03:11
最近由于工作项目中的需要,开始关注起了正则,初识正则,看着着急的同时又表示一头雾水。。。还好现在工作已经告一段落了,非常非常感谢坛子里面前辈的帮助,当然也有自身的学习。O(∩_∩)O
工作之余,把所用到的正则简单的整理了下,备忘,同时也给遇到类似问题的童鞋做个参考。
规则:
1.根据id获取标签里面的内容
2.获取页面中的img及img里面的属性src的内容
3.获取所有的script
4.特殊取值:如取 DATA.groupList = [{...}]里面的内容 废话不多说了,直接贴代码了。。
下面这个是要用到的一个字符串:
<img class="avatar" src="/cgi-bin/getheadimg?fakeid=2391433120&r=835720"><script type="text/javascript">WXM.DATA.userinfo = {};WXM.DATA = {ROOT : WXM.ROOT,userinfo : {NickName : "yoyo",FakeID : "2391433120"},nav : [{_id: 'home',name : "首页", link : '/cgi-bin/indexpage?t=wxm-index&lang=zh_CN'}], };</script> <script type="json" id="json-setting">{"username":"haha","signature":"","country":"中国","province":"上海","city":"浦东新区","verifyInfo":"ready go?","bindUserName":""}</script> <script type="text/javascript">window.WXM && (function(WXM, jq, win){ DATA.title = "用户管理"; DATA.groupList = [ { id: '0', name: defaultGroupName[0] || "默认组", num : '0'*1 }, { id: '100', name: defaultGroupName[100] || "F", num : '2'*1 } ]; })(WXM, jQuery, window);</script> <script type="text/javascript">WXM.DATA.userinfo = {};WXM.DATA = {ROOT : WXM.ROOT,userinfo : {NickName : "yoyo",FakeID : "2391433120"},nav : [{_id: 'home',name : "首页", link : '/cgi-bin/indexpage?t=wxm-index&lang=zh_CN'}]
};</script>
完整的代码如下:
C#:codeusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Text;using System.Text.RegularExpressions;public partial class zz : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { //*****查找id="json-setting"的标签及内容 此处获取到的是一个script,所以无法在页面上显示文本,只有查看网页源码才能看到*****/ string pattern = @"<(?<HtmlTag>[\w]+)[^>]*\s[iI][dD]=(?<Quote>[""']?)json-setting(?(Quote)\k<Quote>)[""']?[^>]*>((?<Nested><\k<HtmlTag>[^>]*>)|</\k<HtmlTag>>(?<-Nested>)|.*?)*</\k<HtmlTag>>"; //*****查找 DATA.groupList = [{...}]里面的内容*****/ //string pattern = @"(?<=DATA\.groupList\s*=\s*\[)((?<g>\[)|(?<-g>\])|[^\]\[])*(?(g)(?!))(?=\])"; //*****查找出所有的script*****// string pattern1 = @"<script[^>]*?>.*?</script>|<script[^>]*>[\d\D]*?</script>"; //******查找图片******// //string pattern2 = "<img class=\"(.*?)\".*/>"; //帅选出img //string pattern2 = "src=\"(?<value>.*?)\""; //获取图片src的值 MatchCollection m = Regex.Matches(GetHtm(), pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline); string str = ""; if (m.Count > 0) { str = m[0].Groups[0].Value; Response.Write("获取成功!" + str); } else { Response.Write("获取失败!"); } Fn(GetHtm(),pattern1); //Fn(GetHtm(),pattern2); } public void Fn(string strhtm,string pattern) { MatchCollection m = Regex.Matches(strhtm, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline); string str = ""; if (m.Count > 0) { foreach (Match mc in m) { str+= mc.Value; //str+ = mc.Groups["value"].Value; //获取图片src的值 } Response.Write("获取成功!" + str); } else { Response.Write("获取失败!"); } } public string GetHtm() { StringBuilder sb = new StringBuilder(); sb.AppendLine("<img class=\"avatar\" src=\"/cgi-bin/getheadimg?fakeid=2391433120&r=835720\" />"); sb.AppendLine("<script type=\"text/javascript\">"); sb.AppendLine("WXM.DATA.userinfo = {};"); sb.AppendLine("WXM.DATA = {"); sb.AppendLine("ROOT : WXM.ROOT,"); sb.AppendLine("userinfo : {"); sb.AppendLine("NickName : \"yoyo\","); sb.AppendLine("FakeID : \"2391433120\""); sb.AppendLine("},"); sb.AppendLine("nav : ["); sb.AppendLine("{"); sb.AppendLine("_id: 'home',"); sb.AppendLine("name : \"首页\", "); sb.AppendLine("link : '/cgi-bin/indexpage?t=wxm-index&lang=zh_CN'"); sb.AppendLine("}],"); sb.AppendLine(""); sb.AppendLine("};"); sb.AppendLine(""); sb.AppendLine("</script>"); sb.AppendLine(" <script type=\"json\" id=\"json-setting\">"); sb.AppendLine("{\"username\":\"haha\",\"signature\":\"\",\"country\":\"中国\",\"province\":\"上海\",\"city\":\"浦东新"); sb.AppendLine("区\",\"verifyInfo\":\"ready go?\",\"bindUserName\":\"\"}</script>"); sb.AppendLine(""); sb.AppendLine(" <script type=\"text/javascript\">"); sb.AppendLine("window.WXM && (function(WXM, jq, win){"); sb.AppendLine(""); sb.AppendLine(" DATA.title = \"用户管理\";"); sb.AppendLine(" DATA.groupList = ["); sb.AppendLine(" {"); sb.AppendLine(" id: '0',"); sb.AppendLine(" name: defaultGroupName[0] || \"默认组\","); sb.AppendLine(" num : '0'*1"); sb.AppendLine(" }, {"); sb.AppendLine(" id: '100',"); sb.AppendLine(" name: defaultGroupName[100] || \"F\","); sb.AppendLine(" num : '2'*1"); sb.AppendLine(" } "); sb.AppendLine(" ];"); sb.AppendLine(" "); sb.AppendLine("})(WXM, jQuery, window);"); sb.AppendLine("</script>"); sb.AppendLine(""); sb.AppendLine(" <script type=\"text/javascript\">"); sb.AppendLine("WXM.DATA.userinfo = {};"); sb.AppendLine("WXM.DATA = {"); sb.AppendLine("ROOT : WXM.ROOT,"); sb.AppendLine(""); sb.AppendLine("userinfo : {"); sb.AppendLine("NickName : \"yoyo\","); sb.AppendLine("FakeID : \"2391433120\""); sb.AppendLine("},"); sb.AppendLine("nav : ["); sb.AppendLine("{"); sb.AppendLine("_id: 'home',"); sb.AppendLine("name : \"首页\", "); sb.AppendLine("link : '/cgi-bin/indexpage?t=wxm-index&lang=zh_CN'"); sb.AppendLine("}"); sb.AppendLine("]"); sb.AppendLine("};</script> "); return sb.ToString(); }}
- 正则的那些事,备忘...
- 几个正则表达式的备忘
- 正则表达那些事
- 正则 备忘
- 正则备忘
- 正则表达式的那些用法
- 备忘!WP8开发必须注意的那些事……[未完待续]
- 备忘:标准的email验证正则表达式
- UltraEdit的正则表达示备忘
- 正则的那些事儿(未完待续)
- 正则表达式备忘
- 正则式学习备忘
- 正则表达式--备忘
- JS正则函数备忘
- c#正则表达式备忘
- 正则表达式整理备忘
- 正则表达式备忘笔记
- 正则表达式 备忘 查询
- ramdisk.img unpack and repack
- Android之SurfaceView学习
- CentOS安装Apache 整ssl
- ORACLE赋予权限
- foxmail不能接收邮件
- 正则的那些事,备忘...
- 家务院总理李德国工作报告
- PHP 数组、关联数组
- 用堆实现求第K大的数
- jsp中打印log日志
- hibernate 计算年月日
- 一种三分频电路的实现与仿真
- c:foreach 详解
- 使用W5200 SPI和SPI+DMA模式下Arduino DUE以太网插板的测试对比