0%

C# 正则提取替换字符串

代码简单,只为方便以后查询,不作注释

            Regex r = new Regex("www.([^\\.]+).com"); 
            string s = "afsasfwww.pocketdigi.comasdfasdfsa";
            MatchCollection mc = r.Matches(s);
            for (int i = 0; i < mc.Count; i++) 
            {
                GroupCollection gc = mc[i].Groups; 
                Console.WriteLine(gc[0]);
                Console.WriteLine(gc[1]);
                s = s.Replace(gc[0].ToString(), "正则替换");
                Console.WriteLine(s);

            }