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>