正则提取 html 里 标记的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();


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM