C#正则表达式获取下拉菜单(select)的相关属性值
C#正则表达式获取下拉菜单(select)的相关属性值
发布时间:2016-12-28 来源:查字典编辑
摘要:给几个在C#中,使用正则表达式取页面下拉菜单(select)中的值示例:复制代码代码如下://取html中全部select的nameRege...

给几个在C#中,使用正则表达式取页面下拉菜单(select)中的值示例:

复制代码 代码如下:

//取html中全部 select 的 name

Regex reg_name = new Regex(@"(?<=<select name="").*");

//取html中全部<select>项的值

Regex reg_select = new Regex("(?is)<select name=*.*?>]*.*");

//取html中一个 select name 等于"Status"的值

Regex status = new Regex(@"(?is)<select name=""status"">]*.*");

一下是一段完整的代码和方法,取html中一个下拉菜单 select name 等于”Status”的中值,添加到DropDownList中:

复制代码 代码如下:

string strDoc = (你的html);

//取html中一个下拉菜单 select name 等于"Status"的中值

Regex status = new Regex(@"(?is)<select name=""status"">]*.*");

MatchCollection mc_status = status.Matches(strDoc);

getSelectOptions(mc_status, cmbStatus);

/// <summary>

/// 取select对列表复制

/// </summary>

/// <param name="selected"></param>

/// <param name="cmb"></param>

void getSelectOptions(MatchCollection selected, ComboBox cmb)

{

if (selected.Count < 1)

return;

txtValues.Text = "";

txtValues.Text = selected[0].Value.Replace("</option>", Environment.NewLine);

string tmpTxt = "";

foreach (string s in txtValues.Lines)

{

if (s == "")

continue;

string a = "";

a = s.Replace(""", "").Replace("<option value="", "");

int x = a.LastIndexOf(">");

tmpTxt += a.Substring(x + 1) + Environment.NewLine;

}

txtValues.Text = tmpTxt.Trim();

cmb.Items.Clear();

cmb.Items.AddRange(txtValues.Lines);

cmb.SelectedIndex = 0;

cmb.Size = cmb.PreferredSize;

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类