正则的那些事,备忘...

 

最近由于工作项目中的需要,开始关注起了正则,初识正则,看着着急的同时又表示一头雾水。。。还好现在工作已经告一段落了,非常非常感谢坛子里面前辈的帮助,当然也有自身的学习。O(∩_∩)O

工作之余,把所用到的正则简单的整理了下,备忘,同时也给遇到类似问题的童鞋做个参考。

规则:

1.根据id获取标签里面的内容

2.获取页面中的img及img里面的属性src的内容

3.获取所有的script

4.特殊取值:如取 DATA.groupList = [{...}]里面的内容废话不多说了,直接贴代码了。。

下面这个是要用到的一个字符串:

<img class="avatar" src="/cgi-bin/getheadimg?fakeid=2391433120&amp;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&nbsp;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,link : '/cgi-bin/indexpage?t=wxm-index&lang=zh_CN'}]

};</script>

完整的代码如下:

C#:code

using 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,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\":\"readygo?\",\"bindUserName\":\"\"}</script>");
        sb.AppendLine("");
        sb.AppendLine(" <script type=\"text/javascript\">");
        sb.AppendLine("window.WXM && (function(WXM,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,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("link : '/cgi-bin/indexpage?t=wxm-index&lang=zh_CN'");
        sb.AppendLine("}");
        sb.AppendLine("]");
        sb.AppendLine("};</script> ");
        return sb.ToString();
       
    }

}

相关文章

jquery.validate使用攻略(表单校验) 目录 jquery.validate...
/\s+/g和/\s/g的区别 正则表达式/\s+/g...
自整理几个jquery.Validate验证正则: 1. 只能输入数字和字母...
this.optional(element)的用法 this.optional(element)是jqu...
jQuery.validate 表单动态验证 实际上jQuery.validate提供了...
自定义验证之这能输入数字(包括小数 负数 ) &lt;script ...