Validating IPv4 string in Java

2019-01-08 22:34发布

问题:

Bellow method is validating if string is correct IPv4 address it returns true if it is valid. Any improvements in regex and elegance would be very appreciated:

public static boolean validIP(String ip) {
    if (ip == null || ip.isEmpty()) return false;
    ip = ip.trim();
    if ((ip.length() < 6) & (ip.length() > 15)) return false;

    try {
        Pattern pattern = Pattern.compile("^(?:(?: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]?)$");
        Matcher matcher = pattern.matcher(ip);
        return matcher.matches();
    } catch (PatternSyntaxException ex) {
        return false;
    }
}

回答1:

Here is an easier-to-read, slightly less efficient, way you could go about it.

public static boolean validIP (String ip) {
    try {
        if ( ip == null || ip.isEmpty() ) {
            return false;
        }

        String[] parts = ip.split( "\\." );
        if ( parts.length != 4 ) {
            return false;
        }

        for ( String s : parts ) {
            int i = Integer.parseInt( s );
            if ( (i < 0) || (i > 255) ) {
                return false;
            }
        }
        if ( ip.endsWith(".") ) {
            return false;
        }

        return true;
    } catch (NumberFormatException nfe) {
        return false;
    }
}


回答2:

UPDATE: Commons-HttpClient and its successor HttpComponents-HttpClient have adopted this functionality. You can utilize this version of it like so: InetAddressUtils.isIPv4Address(Str).


The development version of Apache Commons Validator has a InetAddressValidator class which has a isValidInet4Address(String) method to perform a check to see that an given String is a valid IPv4 address.

The source code can be viewed from the repository, so that might provide some ideas for improvements, if you feel there are any.

A quick glance at the provided code shows that your method is compiling a Pattern on each invocation of the method. I would move that Pattern class out to a static field to avoid the costly pattern compilation process on each call.



回答3:

If you want to use a library with a released version suppporting both IPv4 and IPv6 support, you can use Guava

boolean isValid = InetAddresses.isInetAddress("1.2.3.4");


回答4:

Here's a solution that uses Java 8 streams to check that the address consists of exactly 4 numbers between 0 and 255 inclusive, separated by dots:

public class IpValidation {

    /**
     * Check that a string represents a decimal number
     * @param string The string to check
     * @return true if string consists of only numbers without leading zeroes, false otherwise
     */
    public static boolean isDecimal(String string) {
        if (string.startsWith("0")) {
            if (string.length() == 1) {
                 // "0"
                 return true;
            }
            // The string has a leading zero but is not "0"
            return false;
        }
        for(char c : string.toCharArray()) {
            if(c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }

    public static boolean isIp(String string) {
        String[] parts = string.split("\\.", -1);
        return parts.length == 4 // 4 parts
                && Arrays.stream(parts)
                .filter(IpValidation::isDecimal) // Only decimal numbers
                .map(Integer::parseInt)
                .filter(i -> i <= 255 && i >= 0) // Must be inside [0, 255]
                .count() == 4; // 4 numerical parts inside [0, 255]
    }
}


回答5:

If you don't mind using dns resolution on invalid ip-addresses like www.example.com, you can use the InetAddress methods to check:

public static final boolean checkIPv4(final String ip) {
    boolean isIPv4;
    try {
    final InetAddress inet = InetAddress.getByName(ip);
    isIPv4 = inet.getHostAddress().equals(ip)
            && inet instanceof Inet4Address;
    } catch (final UnknownHostException e) {
    isIPv4 = false;
    }
    return isIPv4;
}

The method checks if it is an instance of Inet4Address and if the parameter was the ip-address and not the hostname. If you expect a lot of hostnames as parameters, beware that this implementation uses DNS to try to resolve it. This might be a performance concern.

Otherwise you can have a peak into boolean sun.net.util.IPAddressUtil.isIPv4LiteralAddress(String src) how the String is parsed there for IPv4-check.



回答6:

Find regex example from this

package com.mkyong.regex;

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

public class IPAddressValidator{

    private Pattern pattern;
    private Matcher matcher;

    private static final String IPADDRESS_PATTERN = 
        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

    public IPAddressValidator(){
      pattern = Pattern.compile(IPADDRESS_PATTERN);
    }

   /**
    * Validate ip address with regular expression
    * @param ip ip address for validation
    * @return true valid ip address, false invalid ip address
    */
    public boolean validate(final String ip){         
      matcher = pattern.matcher(ip);
      return matcher.matches();             
    }
}


回答7:

In think this solution is quite elegant, although it takes a minute to understand it.

The basic idea is to take a sub-string and than validate it.
Please note that I switch x and y, since the start of a sub-string will always be the end of the last one plus 1.

This solution is about 20x faster than a regex variant and 2x faster than splitting.

public static boolean validIP(String ip) {
    if(ip == null || ip.length() < 7 || ip.length() > 15) return false;

    try {
        int x = 0;
        int y = ip.indexOf('.');

        if (y == -1 || ip.charAt(x) == '-' || Integer.parseInt(ip.substring(x, y)) > 255) return false;

        x = ip.indexOf('.', ++y);
        if (x == -1 || ip.charAt(y) == '-' || Integer.parseInt(ip.substring(y, x)) > 255) return false;

        y = ip.indexOf('.', ++x);
        return  !(y == -1 ||
                ip.charAt(x) == '-' ||
                Integer.parseInt(ip.substring(x, y)) > 255 ||
                ip.charAt(++y) == '-' ||
                Integer.parseInt(ip.substring(y, ip.length())) > 255 ||
                ip.charAt(ip.length()-1) == '.');

    } catch (NumberFormatException e) {
        return false;
    }
}

If you know that you'll have a lot wrong IPs consider adding the following code below the first if. This will make the code 1.5x slower but in case of an error improve it by 700x

    for (int i = 0; i < ip.length(); i++) {
        if (!Character.isDigit(ip.charAt(i)) && ip.charAt(i) != '.') return false;
    }


回答8:

If you use the code in the question, you'll want to change the line:

if ((ip.length() < 6) & (ip.length() > 15)) return false;

to

if ((ip.length() <= 6) || (ip.length() > 15)) return false;


回答9:

Try using the following regrex for IPv4

String ip4Regex="^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}$";

hope it helps :)



回答10:

I think the proper way is to build an IPAddress class and instanciate it by giving it a string representation of the the ip address.

This way you can split out the different validation steps into instance methods and get some separation of concern going.

For instance, this here is typically it's own method, call this simply isEmpty:

return (ip == null || ip.isEmpty());

Also this should be a separate method, you could call this one hasProbableLength.

ip = ip.trim();
return ((ip.length() < 6) & (ip.length() > 15));

Here there are lots of things going on. I would try to break this up and perhaps try to skip the regex completely.

try {
    Pattern pattern = Pattern.compile("^(?:(?: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]?)$");
    Matcher matcher = pattern.matcher(ip);
    return matcher.matches();
} catch (PatternSyntaxException ex) {
    return false;
}

I would first split the string on dots and see that I get exactly four groups. Call this method divideIntoGroups

I would then validate each of the groups for being a value between 0 and 255. Call this method validateGroups

Now that you have this, if you ever want to extend this class to also look for if the IP is localhost or if it is a broadcast address, it is pretty easy to do this. This is what separation of concerns gives you.

You can also tell exactly which one of your validation rules was broken in the validation of the IP address string. Something that regex will not.



回答11:

Most simple way I found to check given ip is correctly in ipv4 is to use commons-validator-1.4.0.jar has class:--

org.apache.commons.validator.routines.InetAddressValidator

InetAddressValidator

E.g.

InetAddressValidator inetValidator = InetAddressValidator.getInstance();
inetValidator.isValidInet4Address(yourIPAddress);

which uses this simple regex :--

  "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";


回答12:

Most of the answers (except that of Michael Konietzka) are wrong: 10.8, for instance, is a valid IP4 address (a shorthand for 10.0.0.8).

SeeTextual representation of IP addresses in the Java specifications.

As may be seen in the reference, to check a numeric representation, there may be 1 to 4 parts, and in each case different limitations on the parts apply.



回答13:

I would suggest using what's available on the platform/framework you are developing.

If there's nothing you consider good enough, then you might just want to add a compiled regex to a helper class. For instance, here's how the Android framework does it:

public static final Pattern IP_ADDRESS
    = Pattern.compile(
        "((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
        + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
        + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
        + "|[1-9][0-9]|[0-9]))");


回答14:

boolean isValid = inetAddressValidator.isValid(hostName) || hostName.equalsIgnoreCase("localhost");

same answer as coobird. Just add localhost to the statement. Most of the environments takes 'localhost' as a valid host name. It is not a ipv4 format but it should be included to consider validity.



回答15:

public static boolean ipv4Check(String ipAddress){

    try{
        if(ipAddress!=null && !ipAddress.isEmpty()){
            String [] ip = ipAddress.split("\\.");
            if(ip.length!=4)
                return false;

            for(int i=0;i<=ip.length-1;i++){
                int j=Integer.parseInt(ip[i]);
                if(j<0 || j>255){
                    return false;
                }
            }
            if ( ipAddress.endsWith(".") ) {
                return false;
            }
            if ( ipAddress.startsWith(".") ) {
                return false;
            }

        }
        return true;
    } catch (NumberFormatException ex) {
        return false;
    }       
}


回答16:

The IPAddress Java library will do it. The javadoc is available at the link. Disclaimer: I am the project manager.

This library supports IPv4 and IPv6 transparently, so validating either works the same below, and it also supports CIDR prefix-length IPC4 addresses as well. It supports the more unusual formats like inet_aton (for example 10.8 was mentioned in another answer here)

Verify if an address is valid:

    String str = "1.2.3.4";
    IPAddressString addrString = new IPAddressString(str);
    try {
         IPAddress addr = addrString.toAddress();
         ...
    } catch(AddressStringException e) {
        //e.getMessage provides validation issue
    }