usr021986
usr021986

Reputation: 3511

Validate IPAddress.TryParse with network mask

I want to validate(just check if the IP address is in correct format with and without network mask) the IP address which has network mask say example : 192.168.0.254/32. This should give result as valid IP address.

I can verify it with this Regex expression Regex(@"\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b/\d{1,3}\b|\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b");

However can the IP address be verified by IPAddress.TryParse or any other existing method ?

Edit to confirm for not similar question : Here i want to check if the IP address is in correct format with and without network mask in one check

Upvotes: 0

Views: 497

Answers (1)

NetMage
NetMage

Reputation: 26907

Here is a pattern you can use to validate a string represents a valid four octet IP address, with an optional net mask, using regex:

(?:(?:[0-2]\d\d|\d\d?)\.){3}(?:[0-2]\d\d|\d\d?)(?:/(?:3[0-2]|[0-2]\d|\d))?

Upvotes: 1

Related Questions