Regex for IP address

后端 未结 6 1274
失恋的感觉
失恋的感觉 2020-11-28 14:59

I tried this code for validating IP address, but it doesn\'t work...

public static bool IP(string ipStr)
{
    string pattern = @\"^([1-9]|[1-9][0-9]|1[0-9][         


        
6条回答
  •  醉梦人生
    2020-11-28 15:23

    Here is the complete regex for checking all possible IPv4 addresses (dotted decimal, dotted hexadecimal, dotted octal, decimal, hexadecimal, octal and mixed mode)

    ^(0[0-7]{10,11}|0(x|X)[0-9a-fA-F]{8}|(\b4\d{8}[0-5]\b|\b[1-3]?\d{8}\d?\b)|((2[0-5][0-5]|1\d{2}|[1-9]\d?)|(0(x|X)[0-9a-fA-F]{2})|(0[0-7]{3}))(\.((2[0-5][0-5]|1\d{2}|\d\d?)|(0(x|X)[0-9a-fA-F]{2})|(0[0-7]{3}))){3})$
    

    I have tested it with all possible addresses.

提交回复
热议问题