Regex for IP address

后端 未结 6 1286
失恋的感觉
失恋的感觉 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:18

    I'm not really a regex expert per se but i use Expresso (a regex tool) and it has it's own regex library for pre-set scenarios like this. Try this below.

    string pattern = @"(?2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?2[0-4]\d|25[0-5]|[01]?\d\d?)";
    

提交回复
热议问题