前言:
正则应用在做判断、截取、过滤有很好的效果,记录一下使用过的方法,能给程序带来很大的方便。
组合1 (.*?)
//style所有属性替换成指定 string regtxt = "style=\"(.*?)\""; Regex reg = new Regex(regtxt, RegexOptions.IgnoreCase); string xhtml2 = reg.Replace(xhtml, "style=\"width:95%;margin:10px;\""); Content = Content.Replace(xhtml, xhtml2);
组合2 (?<=)
string input = "<div><img src=\"123123.jpg\" /></div>"; string pattern = @"(?<=<img(.*)src="").*?(?="")"; foreach (Match match in Regex.Matches(input, pattern)) Console.WriteLine(match.Value);