RegEx for an IP Address

前端 未结 12 1458
不知归路
不知归路 2020-11-29 03:52

I try to extract the value (IP Address) of the wan_ip with this sourcecode: Whats wrong?! I´m sure that the RegEx pattern is correct.

String input = @\"var p         


        
相关标签:
12条回答
  • 2020-11-29 04:16
    Regex.IsMatch(input, @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$")
    
    0 讨论(0)
  • 2020-11-29 04:20

    If you just want check correct IP use IPAddress.TryParse

    using System.Net;
    
    bool isIP(string host)
    {
        IPAddress ip;
        return IPAddress.TryParse(host, out ip);
    }
    
    0 讨论(0)
  • 2020-11-29 04:21

    Very old post, you should use the accepted solution, but consider using the right RegEx for an IPV4 adress :

    ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
    

    If you want to avoid special caracters after or before you can use :

    ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
    
    0 讨论(0)
  • 2020-11-29 04:21
    (\d{1,3}\.){3}(\d{1,3})[^\d\.] 
    

    Check this. It should work perfectly

    0 讨论(0)
  • 2020-11-29 04:26

    Regex(@"\A\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\z") try with this

    0 讨论(0)
  • 2020-11-29 04:30

    In Python:

    >>> ip_regex = r'^{0}\.{0}\.{0}\.{0}$'.format(r'(25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)')
    >>> match(ip_regex, '10.11.12.13')
    <re.Match object; span=(0, 11), match='10.11.12.13'>
    >>> _.groups()
    ('10', '11', '12', '13')
    >>>
    
    0 讨论(0)
提交回复
热议问题