在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
string content = "网站内容"; string pattern = @"<a[\s\S]*?href=(""(?<href>[^""]*)""|’(?<href>[^’]*)’|(?<href>[^>\s]*))[^>]*?>"; MatchCollection mc = Regex.Matches(content,pattern,RegexOptions.IgnoreCase|RegexOptions.Compiled); foreach (Match m in mc) { string url = m.Groups["href"].Value; string replaceUrl = "http://www.baidu.com/" + "?to=" + url; content = Regex.Replace(content,url,replaceUrl); }
第二种 string content = "网站内容"; string pattern = @"<a[\s\S]*?href=(""(?<href>[^""]*)""|’(?<href>[^’]*)’|(?<href>[^>\s]*))[^>]*?>"; string repStr = "<a target=\"_blank\" href=\"http://www.baidu.com/?to=${href}\">"; content = Regex.Replace(content,pattern,repStr,RegexOptions.IgnoreCase|RegexOptions.Compiled); 参考内容: public static void Main() { string s = "1 12 3 5"; s = Regex.Replace(s,@"\d+",new MatchEvaluator(CorrectString),RegexOptions.Compiled|RegexOptions.IgnoreCase); Console.WriteLine(s); Console.ReadLine(); } private static string CorrectString(Match match) { string matchValue = match.Value; if(matchValue.Length == 1) matchValue = "0" + matchValue; return matchValue; }
public static void Main() { string s = "1 12 3 5"; s = Regex.Replace(s,@"(\d+)(?#这个是注释)","0$1",RegexOptions.Compiled|RegexOptions.IgnoreCase); Console.WriteLine(s); Console.ReadLine(); } 这段代码返回的是 “01 012 03 05”
|
请发表评论