格码拓普是一个专注于教育管理类、现场比赛展示类、智能办公类平台应用的研发团队。该团队采用了知识管理理念,已凝聚成一个"格调高远、码力十足"的组织。
图片地址替换是一个常规应用,下方是后端代码演示。 NetCore开发师
参考代码:
using System; using System.Text.RegularExpressions; namespace RegexDemo { class Program { static void Main(string[] args) { //string phonebook = "(010)88665987 (020)23658945 (021)88965222"; //string pattern = @"\((\d{3,4})\)(\d{7,8})"; //string result = Regex.Replace(phonebook, pattern, "$1-$2"); //Console.WriteLine(result); //string phonebook2 = "(010)88665987 (020)23658945 (021)88965222"; //string pattern2 = @"\((?<areacode>\d{3})\)(?<number>\d{8})"; //string result2 = Regex.Replace(phonebook2, pattern2, "${areacode}-${number}"); //Console.WriteLine(result2); string imgUrl ="src=\"https://pic.dshow.net/upload/123344.jpg\""; string imgUrl2 = "src=\"http://pic.dshow.net/upload/123344.jpg\""; string imgUrl3 = "/upload/agafb/afweff_123344.jpg"; string imgUrl4 = "src=\"/upload/abd1234_343r_3435/123344.jpg\""; Regex reg = new Regex(@"/upload(/(\w)+)+\.(png|gif|jpg|jpeg|jpg)"); Regex reg2 = new Regex(@"src=""/upload(/(\w)+)+\.(png|gif|jpg|jpeg|jpg)"""); Regex reg3 = new Regex(@"src=""/upload"); Console.WriteLine(reg.IsMatch(imgUrl3)); Console.WriteLine(reg2.IsMatch(imgUrl4)); var rrr = reg2.Matches(imgUrl4); foreach(var e in rrr) { Console.WriteLine("1.匹配到的内容为:" + e.ToString()); } string input = "这是一段\"包含双引号\"的文本"; string pattern = @"""([^""]*)"""; MatchCollection matches = Regex.Matches(input, pattern); foreach (Match match in matches) { Console.WriteLine("2.匹配到的内容为:" + match.Value); } string s4 = reg3.Replace(imgUrl4, @"src=""http://localhost/upload"); Console.WriteLine("3.替换后:" + s4); } } }
良好的沟通,是成功的一半
博客评论