把一个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.*.*
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.*.*
string ip = "192.168.255.255";
result = Regex.Replace(ip, @"(?<=\d+\.\d+\.)\d+\.\d+", "*.*");
Console.WriteLine(result);
string ip = "192.168.255.255";
string result =string.Join(".",ip.Split('.').Take(2).ToArray())+".*.*";