C# 正則表達式html匹配input標籤及匹配input的value及獲取aspnet頁面VIEWSTATE、EVENTVALIDATION的UI狀態保存值

1、使用正則表達式從html內容中獲取 input 標籤,然後從 input 標籤中獲取屬性值;

也可以使用正則表達式捕獲組獲取value,在2中說明

using System.Text.RegularExpressions;

string excelHtml = divMX.InnerHtml;

        Regex reg_Value = new Regex("value=\'.*\'");
        Match match_Value = null;

        Regex reg = new Regex("<input[^<>]+></input>");
        MatchCollection matchCollect = reg.Matches(excelHtml);

        foreach (Match match in matchCollect)
        {
            match_Value = reg_Value.Match(match.Value);
            excelHtml = excelHtml.Replace(match.Value, match_Value.Value.Split('=')[1].Replace('\'',' '));
        }


         reg = new Regex("<input[^<>]+/>");
         matchCollect = reg.Matches(excelHtml);

         foreach (Match match in matchCollect)
         {
             match_Value = reg_Value.Match(match.Value);
             excelHtml = excelHtml.Replace(match.Value, match_Value.Value.Split('=')[1].Replace('\'', ' '));
         }

2、獲取aspnet頁面VIEWSTATE、EVENTVALIDATION的值,通過正則表達式匹配組的概念

/// <summary>
    /// 獲取asp.net頁面的隱藏控件值(狀態信息與驗證信息等)
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    [WebMethod]
    public string GetHiddenField(string url)
    {
        System.Net.WebRequest request = System.Net.WebRequest.Create(url);//獲取頁面內容
        request.Timeout = 200000;//20秒超時 
        System.Net.WebResponse response = request.GetResponse();
        Stream resStream = response.GetResponseStream();
        StreamReader sr = new StreamReader(resStream);
        string tempstr = sr.ReadToEnd();

        //Regex reg = new Regex("<input type=\"hidden\" name=\"(.*?)\" id=\"(.*?)\" value=\"(.*?)\" />", RegexOptions.IgnoreCase);

        string matchValue = string.Empty;
        string val_VIEWSTATE = string.Empty;
        Regex reg_VIEWSTATE = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" />", RegexOptions.IgnoreCase);
        Match match = reg_VIEWSTATE.Match(tempstr);
        if (match.Success)
        {
             matchValue = match.Value;//匹配的字符串
             val_VIEWSTATE = match.Groups[1].Value; //匹配的value值,正則表達式中的第一個括號
        }

        string val_VIEWSTATEGENERATOR = string.Empty;
        Regex reg_VIEWSTATEGENERATOR = new Regex("<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" />", RegexOptions.IgnoreCase);
        match = reg_VIEWSTATEGENERATOR.Match(tempstr);
        if (match.Success)
        {
            matchValue = match.Value;//匹配的字符串
            val_VIEWSTATEGENERATOR = match.Groups[1].Value; //匹配的value值,正則表達式中的第一個括號
        }

        string val_EVENTVALIDATION = string.Empty;
        Regex reg_EVENTVALIDATION = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" />", RegexOptions.IgnoreCase);
        match = reg_EVENTVALIDATION.Match(tempstr);
        if (match.Success)
        {
            matchValue = match.Value;//匹配的字符串
            val_EVENTVALIDATION = match.Groups[1].Value; //匹配的value值,正則表達式中的第一個括號
        }

        //MatchCollection mCollection = r6.Matches(tempstr);
        //if (mCollection != null && mCollection.Count > 0)
        //{
        //    val = mCollection[0].Value;
        //    Regex reg_Value = new Regex("value=\".*\"");
        //    Match match_Value = reg_Value.Match(val);
        //    string value = match_Value.Value.Split('=')[1].Replace("\"", "");
        //}

        return tempstr;
    }

3、javascript獲取方法

<script type="text/javascript" >
var str="<input  type=\"text\" value=\"12345688\" />";
var ze=/value="(.+?)"/
str=str.match(ze);
alert("value值等於:"+str[1])//同樣通過捕獲組的原理,獲取第一個括號內的值,str[0]匹配整個表達式,即value=\"12345688\"
</script>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章