正则提取 html 里<input> 标记的value 值

获取html 标记的值:

:

结果:您选择的是2014年1月22日

使用了Regex 对象,得到一个 MatchCollection,然后进行处理。

string mes = @"<input value='您选择的是' type='checkbox' size=5 name=年 >:<input value=2014 size=5 name=年 >年<input value=1 size=5 name=月 >月<input value='22'  size=5 name='日' >日";
            //获取所有的<input>标记
            Regex regAllInput = new Regex(@"(?is)<input [^>]*>");
            //获取 <input value=''>的标记 
            //?value=(['""]?)(?<showValue>[^'""s>]+)1  给value 的值取个标记 showValue,MatchCollection 时候方便获取
            Regex regValue = new Regex(@"(?is)<input ?value=(['""]?)(?<showValue>[^'""s>]+)1 [^>]*>");
            MatchCollection mc = regAllInput.Matches(mes);//所有<input >标签集合
            string value_temp=string.Empty;
            foreach (Match m in mc)
            {
                //所有<input value='' >标签集合
                MatchCollection mcItem = regValue.Matches(m.ToString());
                foreach (Match item in mcItem)
                {
                    value_temp=item.Groups["showValue"].Value;//获取value 值
                }               
                mes = mes.Replace(m.ToString(), value_temp);//进行替换
            }
            Console.WriteLine(mes);
            Console.ReadLine();

原文地址:https://www.cnblogs.com/jak-black/p/3690401.html