C#正則表達(dá)式獲取下拉菜單(select)的相關(guān)屬性值
給幾個在C#中,使用正則表達(dá)式取頁面下拉菜單(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對列表復(fù)制
/// </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;
}
相關(guān)文章
用C#對ADO.NET數(shù)據(jù)庫完成簡單操作的方法
用C#對ADO.NET數(shù)據(jù)庫完成簡單操作的方法...2007-03-03C#實現(xiàn)windows系統(tǒng)重啟和關(guān)機的代碼詳解
這篇文章主要介紹了C#實現(xiàn)windows系統(tǒng)重啟和關(guān)機的的方法,涉及C#調(diào)用windows系統(tǒng)命令實現(xiàn)控制開機、關(guān)機等操作的技巧,非常簡單實用,需要的朋友可以參考下2024-02-02