给几个在C#中,使用正则表达式取页面下拉菜单(select)中的值示例:
复制代码 代码如下: //取html中全部 select 的 name Regex reg_name = new Regex(@"(?<= //取html中全部项的值 Regex reg_select = new Regex("(?is)]*.*?"); //取html中一个 select name 等于"Status"的值 Regex status = new Regex(@"(?is)]*.*?"); 一下是一段完整的代码和方法,取html中一个下拉菜单 select name 等于”Status”的中值,添加到DropDownList中: 复制代码 代码如下: string strDoc = (你的html); //取html中一个下拉菜单 select name 等于"Status"的中值 Regex status = new Regex(@"(?is)]*.*?"); MatchCollection mc_status = status.Matches(strDoc); getSelectOptions(mc_status, cmbStatus); /// /// 取select对列表复制 /// /// /// void getSelectOptions(MatchCollection selected, ComboBox cmb) { if (selected.Count < 1) return; txtValues.Text = ""; txtValues.Text = selected[0].Value.Replace("", Environment.newline); string tmpTxt = ""; foreach (string s in txtValues.Lines) { if (s == "") continue; string a = ""; a = s.Replace(""", "").Replace(" 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; }
//取html中全部项的值 Regex reg_select = new Regex("(?is)]*.*?");
//取html中一个 select name 等于"Status"的值 Regex status = new Regex(@"(?is)]*.*?");
一下是一段完整的代码和方法,取html中一个下拉菜单 select name 等于”Status”的中值,添加到DropDownList中:
复制代码 代码如下: string strDoc = (你的html);
//取html中一个下拉菜单 select name 等于"Status"的中值 Regex status = new Regex(@"(?is)]*.*?"); MatchCollection mc_status = status.Matches(strDoc); getSelectOptions(mc_status, cmbStatus);
/// /// 取select对列表复制 /// /// /// void getSelectOptions(MatchCollection selected, ComboBox cmb) { if (selected.Count < 1) return; txtValues.Text = ""; txtValues.Text = selected[0].Value.Replace("", Environment.newline); string tmpTxt = ""; foreach (string s in txtValues.Lines) { if (s == "") continue; string a = ""; a = s.Replace(""", "").Replace(" 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#中禁用windows的任务管理器的方法
下一篇 C#实现窗体间传递数据实例
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号