IP Address Validation

Sort by

recency

|

221 Discussions

|

  • + 0 comments

    perl

    use capture groups to test that IPv4 digits are valid

    while(<>){
        chomp;
        next if $. == 1;
        if (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){
        ($1 < 256 && $2 < 256 && $3 < 256 && $4 < 256) ? print "IPv4\n" : print "Neither\n";
            
        } elsif ( /^([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4})$/ ){
            print "IPv6\n";
        } else {
            print "Neither\n";
        }
    
  • + 0 comments

    Real world solution (avoid doing such things manually):

    from ipaddress import ip_address
    
    for _ in range(int(input())):
        try:
            addr = ip_address(input())
            print("IPv4" if addr.version == 4 else "IPv6")
        except ValueError:
            print("Neither")
    
  • + 0 comments

    Java 15

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int t = scanner.nextInt(); scanner.nextLine();
            String u = "(25[0-5]|2[0-4]\\d|1?\\d?\\d)"; //0 to 255
            String regex_ipv4 = String.format("^(%s\\.){3}%s$",u,u); //u.u.u.u
            String v = "[a-fA-F0-9]{1,4}"; //4 hexadecimal digits
            String regex_ipv6 = String.format("^(%s\\:){7}%s$",v,v); //v:v:v:v:v:v:v:v (8 groups)
            Pattern pattern_ipv4 = Pattern.compile(regex_ipv4);
            Pattern pattern_ipv6 = Pattern.compile(regex_ipv6);
            for(int i=0;i<t;i++)
            {   String str = scanner.nextLine();
                Matcher matcher_ipv4 = pattern_ipv4.matcher(str);
                Matcher matcher_ipv6 = pattern_ipv6.matcher(str);
                if (matcher_ipv4.find())    System.out.println("IPv4");
                else if (matcher_ipv6.find())   System.out.println("IPv6");
                else    System.out.println("Neither");
            }
        }
    }
    
    /*
    ---------- v = "[a-fA-F0-9]{1,4}" ---------------
    4 hexadecimal digits.
    Hexadecimal means either using a-f or 0-9
    ff translates to 00ff. So no of digits can be either 1,2,3 or 4. So use {1,4}
    
    ---------- u = "(25[0-5]|2[0-4]\\d|1?\\d?\\d)" ---------------
    Means 0 to 255
    3 digit combinations: 
        25[0-5]  or 2[0-4][0-9] or 1[0-9][0-9] 
    2 digit combinations:
        [0-9][0-9]
    1 digit combinations:
        [0-9]
    Combining 1 digit and 2 digit combinations: becomes: [0-9]?[0-9]
        Combine this with 1[0-9][0-9] : becomes : 1?[0-9]?[0-9]
    So final set of combinations : becomes:
        25[0-5]  or 2[0-4][0-9] or 1?[0-9]?[0-9]
    
    
    */
    
  • + 0 comments

    Python 3

    import re
    n = int(input())
    for _ in range(n):
        address = input()
        regexv4 = r"^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$"
        regexv6 = r"^([a-f0-9]{1,4}:){7}([a-f0-9]{1,4})$"
        if re.match(regexv4, address):
            print("IPv4")
        elif re.match(regexv6, address):
            print("IPv6")
        else:
            print("Neither")
    
  • + 0 comments
    import re
    
    pattern_ipv4 = r'^(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])){3}$'
    pattern_ipv6 = r'^([\da-f]{,4})(:([\da-f]{,4})){7}$'
    
    for _ in range(int(input())):
        line = input()
        if re.match(pattern_ipv4, line):
            print('IPv4')
        elif re.match(pattern_ipv6, line):
            print('IPv6')
        else:
            print('Neither')