Java IP Address Validate isIpV6RegexValid(String strIp)

Here you can find the source of isIpV6RegexValid(String strIp)

Description

check if IP v6 string is valid by regex.

License

Open Source License

Parameter

Parameter Description
strIp IP string.

Return

true if valid, false if not.

Declaration

public static boolean isIpV6RegexValid(String strIp) 

Method Source Code


//package com.java2s;
import java.util.regex.Pattern;

public class Main {
    /**/*  w  ww .  j  ava 2s. c o  m*/
     * check if IP v6 string is valid by regex.
     * 
     * @param strIp
     *            IP string.
     * @return true if valid, false if not.
     */
    public static boolean isIpV6RegexValid(String strIp) {
        final String regexV6 = "^([\\da-fA-F]{1,4}:){6}" + "((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}"
                + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|" + "^::([\\da-fA-F]{1,4}:){0,5}"
                + "((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|"
                + "^([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,4}" + "((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}"
                + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|" + "^([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:){0,3}"
                + "((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|"
                + "^([\\da-fA-F]{1,4}:){3}:([\\da-fA-F]{1,4}:){0,2}" + "((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}"
                + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|" + "^([\\da-fA-F]{1,4}:){4}:([\\da-fA-F]{1,4}:){0,1}"
                + "((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|"
                + "^([\\da-fA-F]{1,4}:){5}:" + "((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}"
                + "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|" + "^([\\da-fA-F]{1,4}:){7}[\\da-fA-F]{1,4}$|"
                + "^:((:[\\da-fA-F]{1,4}){1,7}|:)$|" + "^[\\da-fA-F]{1,4}:((:[\\da-fA-F]{1,4}){1,6}|:)$|"
                + "^([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,5}|:)$|"
                + "^([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,4}|:)$|"
                + "^([\\da-fA-F]{1,4}:){4}((:[\\da-fA-F]{1,4}){1,3}|:)$|"
                + "^([\\da-fA-F]{1,4}:){5}((:[\\da-fA-F]{1,4}){1,2}|:)$|"
                + "^([\\da-fA-F]{1,4}:){6}:([\\da-fA-F]{1,4})?$|" + "^([\\da-fA-F]{1,4}:){7}:$";
        Pattern pattern = Pattern.compile(regexV6);
        boolean isRegular = pattern.matcher(strIp).matches();
        return isRegular;
    }
}

Related

  1. isIPv6Address(String _ipAddress)
  2. isIPv6HexCompressedAddress(final String input)
  3. isIPv6HexCompressedAddress(final String input)
  4. isIPv6HexCompressedAddress(String ip)
  5. isIPv6LiteralAddress(String src)
  6. isIPv6StdAddress(final String input)
  7. isIpValid(final String deviceIp)
  8. isIPValid(final String ipv4)
  9. isIpValid(String ip)