0

I've lifted a piece of code from somewhere on the internet it looks like this

 ip = Regex.Replace(ip, @"^(?<Prefix>(\d{1,3}\.){3})\d{1,3}$", "${Prefix}*");

What it does is takes an IP address and replaces the last section with a asterisk. For example 192.168.0.1 would become 192.168.0.*

I'm useless with RegEx, I've tried to understand what the above is actually doing, but not having any success.

What I'm after is 2 more Regex.Replace code so that 192.168.0.1 becomes

  • 192.168.*.*
  • 192.*.*.*

Can anyone help me?

4

1 回答 1

1

192.168.*.* =ip = Regex.Replace(ip, @"^(?<Prefix>(\d{1,3}\.){2})\d{1,3}\.\d{1,3}$", "${Prefix}*.*");

192.*.*.* =ip = Regex.Replace(ip, @"^(?<Prefix>(\d{1,3}\.))\d{1,3}\.\d{1,3}\.\d{1,3}$", "${Prefix}*.*.*");

试一试,看看会发生什么。

于 2012-08-15T15:42:12.520 回答