C#正则表达式获取下拉菜单(select)的相关属性值
给几个在c#中,使用正则表达式取页面下拉菜单(select)中的值示例:
//取html中全部 select 的 name
regex reg_name = new regex(@"(?<=<select name=\"").*?(?=\"")");
//取html中全部<select>项的值
regex reg_select = new regex("(?is)<select name=*.*?>]*.*?</select>");
//取html中一个 select name 等于"status"的值
regex status = new regex(@"(?is)<select name=\""status\"">]*.*?</select>");
一下是一段完整的代码和方法,取html中一个下拉菜单 select name 等于”status”的中值,添加到dropdownlist中:
string strdoc = (你的html);
//取html中一个下拉菜单 select name 等于"status"的中值
regex status = new regex(@"(?is)<select name=\""status\"">]*.*?</select>");
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;
}