日期:2014-05-17 浏览次数:20506 次
<p> 这是一个新闻详细页面 <img alt="" src="/ImgUpload/images/Awards%20-%20K2.png" style="width: 1280px; height: 699px; " /></p>
System.IO.StreamReader reader = new System.IO.StreamReader("e:\\1.txt"); string str = reader.ReadToEnd(); Regex reg = new Regex(@"(?is)(?<=<img[^>]*?)alt=.*?(?=src=[^<]*?/>)"); Match m = reg.Match(str); str = reg.Replace(str, "alt=%title ");
------解决方案--------------------
string str = @"<p> 这是一个新闻详细页面 <img alt="""" src=""/ImgUpload/images/Awards%20-%20K2.png"" style=""width: 1280px; height: 699px; "" /> <img alt=""IMGALT"" src=""/ImgUpload/images/Awards%20-%20K2.png"" style=""width: 1280px; height: 699px; "" /></p>"; string result = Regex.Replace(str, @"(?is)(?<=<img[^>]*?alt=(['""]))(?=\1)", "%title"); result = Regex.Replace(result, @"(?is)(?<=<img\b)", " title=\"title\""); Console.WriteLine(result);