正則提取 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