Java IP Address Validate isIPAddress(final String s)

Here you can find the source of isIPAddress(final String s)

Description

Returns whether the supplied string represents an IP address.

License

Open Source License

Parameter

Parameter Description
s to match

Return

whether the supplied string represents an IP address

Declaration

public static boolean isIPAddress(final String s) 

Method Source Code

//package com.java2s;
/* See LICENSE for licensing and NOTICE for copyright. */

import java.util.regex.Pattern;

public class Main {
    /** Pattern to match ipv4 addresses. */
    private static final Pattern IPV4_PATTERN = Pattern
            .compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)" + "(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
    /** Pattern to match ipv6 addresses. */
    private static final Pattern IPV6_STD_PATTERN = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
    /** Pattern to match ipv6 hex compressed addresses. */
    private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile(
            "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::" + "((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");

    /**//  w  ww  . j  av  a 2  s  .  com
     * Returns whether the supplied string represents an IP address. Matches both IPv4 and IPv6 addresses.
     *
     * @param  s  to match
     *
     * @return  whether the supplied string represents an IP address
     */
    public static boolean isIPAddress(final String s) {
        return s != null && (IPV4_PATTERN.matcher(s).matches() || IPV6_STD_PATTERN.matcher(s).matches()
                || IPV6_HEX_COMPRESSED_PATTERN.matcher(s).matches());
    }
}

Related

  1. isIP(String src)
  2. isIP(String str)
  3. isIp(String str)
  4. isIp(String str)
  5. isIP4(String val)
  6. isIpAddress(String addr)
  7. isIpAddress(String ip)
  8. isIPAddress(String ipAddress)
  9. isIpAddress(String ipAddress)