IP Address Validation

Sort by

recency

|

223 Discussions

|

  • + 0 comments

    PHP Solution:

    <?php
        $input = stream_get_contents($_fp);
        $lines = preg_split('/\R/', $input, -1, PREG_SPLIT_NO_EMPTY);
        array_shift($lines);
        $ipv4_pattern = '/^(([01]?[0-9]{0,2}|2[0-5]?[0-5]?)\.){3}([01]?[0-9]{0,2}|2[0-5]?[0-5]?)$/';
        $ipv6_pattern = '/^(([a-fA-F0-9]{1,4}):){7}[a-fA-F0-9]{1,4}$/';
        foreach($lines as $line) {
            $result = 'Neither';
            if (preg_match($ipv4_pattern, $line)) {
                $result = 'IPv4';
            } elseif (preg_match($ipv6_pattern, $line)) {
                $result = 'IPv6';
            }
            
            echo $result . PHP_EOL;
        }
    
  • + 0 comments

    Must unlock editorial / no credit after a successful submission ???!!

    As mentioned below, regex is not the right tool for identifying valid IP addresses.

    Node use net.isIP

  • + 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]
    
    
    */