IsIPv4LiteralAddress Has False Positive Results?

When I try to check if a string is an IPv4 address or not, I find that the function gives the following results.

144.122.1 → true

144.122.1.a → false

144.122.1.333 → false

Any idea why the first is true?

Function: sun.net.util.IPAddressUtil.isIPv4LiteralAddress

+3
source share
2 answers

You can understand why they did this from the source:

When a three-part address is specified, the last part is interpreted as a 16-bit value and is placed on the right most two bytes of the network address. This makes the three-part address format convenient for specifying Class B network addresses like 128.net.host.

So the expression 144.122.1is equal 144.122.0.1.

IP-:

, - .

, 24- . A net.host.

, 16- . B 128.net.host.

, IPv4.

+7

, , , IP- 0-255.0-255.0-255.0-255

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author ragavan
 */
public class MatchIPv4Address {
    public static void main(String[] args) {
        String range = "(([01][0-9][0-9])|(2([0-4][0-9]|5[0-5]))|([0-9]?[0-9]))";
        String pattern = range+"\\."+range+"\\."+range+"\\."+range;
        System.out.println(pattern);
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher("144.229.1.99");
        System.out.println(m.matches());
    }

}
+2

Source: https://habr.com/ru/post/1776705/


All Articles