把一个IP地址的后两位用*号替换            string result = "";
            string ip = "192.168.255.255";
            Regex reg = new Regex(@"\d+\.\d+\.(\d+)\.(\d+)");
            MatchCollection match = reg.Matches(ip);
            foreach (Match m in match)
            {
                result = m.Result("$1$2");
            }
//这样可以得到255 255,可是要怎么把255替换为*号,并得到最终结果呢?
//就是192.168.*.*

解决方案 »

  1.   

    string result = Regex.Replace(ip,@"\d+\.\d+","*.*");
      

  2.   

    哈。手误string result = Regex.Replace(ip, @"\d+\.\d+$", "*.*");
      

  3.   

                string result = "";
                string ip = "192.168.255.255";
                result = Regex.Replace(ip, @"(?<=\d+\.\d+\.)\d+\.\d+", "*.*");
                Console.WriteLine(result);
      

  4.   


    string ip = "192.168.255.255";
     string result =string.Join(".",ip.Split('.').Take(2).ToArray())+".*.*";
      

  5.   

    哈 tankyou 当时怎么没想到以数字结尾的呢!