正則提取 html 裏 標記的value 值

獲取html 標記的值:html

:spa

結果:您選擇的是2014年1月22日code

使用了Regex 對象,獲得一個 MatchCollection,而後進行處理。htm

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();

相關文章
相關標籤/搜索