Reputation: 13460
I need a C# regular expression that will match an IP Subnet, like "127.65.231", but not match an IP Address on the subnet, like "127.65.231.111". I had found this Regex for an IP Address:
@"\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b"
and was thinking I could just delete the part that checks the last octet, like this:
@"\b\d{1,3}.\d{1,3}.\d{1,3}\b"
but this matches both the IP Address and the Subnet. Can anyone help with this?
Upvotes: 4
Views: 2740
Reputation: 25370
@"^\d{1,3}\.\d{1,3}\.\d{1,3}$"
use Line Anchors. Add ^ at the beginning of your Regex, and $ at the end, to verify the beginning and end of the input.
This will match 127.65.231
but not 127.65.231.111
Upvotes: 0
Reputation: 149050
You might try using a lookahead. Also, please escape the .
characters—otherwise it would match any character:
@"\b\d{1,3}\.\d{1,3}\.\d{1,3}(?=\.\d{1,3})\b"
This will match any string like 127.65.231
as long as it's followed by a string like .111
.
Upvotes: 1