IP Address Validation

Sort by

recency

|

225 Discussions

|

  • + 0 comments

    Great post! I really enjoyed reading your insights on this topic—it’s both informative and thought-provoking. Your perspective reminds me of the importance of giving back, whether through discussions like this or actions like orphanage food donation to support those in need. Keep up the fantastic work, and I look forward to your next article.

  • + 0 comments

    TypeScript or JavaScript

    function main() {
        const regex4ipv4 = /^(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))$/;
        const regex4ipv6 = /^(([0-9a-f]{1,4}):){7}([0-9a-f]{1,4})$/;
        for (let i = 1; i < inputLines.length; i++) {
            const str = inputLines[i];
            if (regex4ipv4.test(str)) {
                console.info('IPv4');
            } else if (regex4ipv6.test(str)) {
                console.info('IPv6');
            } else {
                console.info('Neither');
            }
        }
    }
    
  • + 1 comment

    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

      I think it would not capture 249.249.249.249?

  • + 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";
        }