手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#正则表达式获取下拉菜单(select)的相关属性值
C#正则表达式获取下拉菜单(select)的相关属性值
摘要:给几个在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#正则表达式获取下拉菜单(select)的相关属性值】相关文章:

C# 获取打印机当前状态的方法

C#词法分析器之正则表达式的使用

C#获取全部目录和文件的简单实例

基于C#中XmlWriter写入Xml的深入分析

c# 获取数据库中所有表名称的方法

用.NET创建Windows服务的方法第1/2页

RegexOptions.IgnoreCase正则表达式替换,忽略大小写

C#将dll打包到程序中的具体实现

用C#编写获取远程IP,MAC的方法

c#匹配整数和小数的正则表达式

精品推荐
分类导航