The C regular expression gets the value of the associated property of the drop down menu of select
- 2020-06-19 11:39:48
- OfStack
Here are some examples of values in C# that use regular expressions to fetch from the page drop-down menu (select) :
// take html In all select the name
Regex reg_name = new Regex(@"(?<=<select name=\"").*?(?=\"")");
// take html In all <select> The value of the item
Regex reg_select = new Regex("(?is)<select name=*.*?>]*.*?</select>");
// take html In the 1 a select name Is equal to the "Status" The value of the
Regex status = new Regex(@"(?is)<select name=\""status\"">]*.*?</select>");
1 below is a complete code and method. Take one drop-down menu in html, select name is equal to the median of "Status", and add it to DropDownList:
string strDoc = (your html ) ;
// take html In the 1 A drop-down menu select name Is equal to the "Status" The value of the
Regex status = new Regex(@"(?is)<select name=\""status\"">]*.*?</select>");
MatchCollection mc_status = status.Matches(strDoc);
getSelectOptions(mc_status, cmbStatus);
/// <summary>
/// take select Copy the list
/// </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;
}