[ACCEPTED]-What are the Java regular expressions for matching IPv4 and IPv6 strings?-regex

Accepted answer
Score: 18
public static final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
public static final String IPV6_HEX4DECCOMPRESSED_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::((?:[0-9A-Fa-f]{1,4}:)*)(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
public static final String IPV6_6HEX4DEC_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}:){6,6})(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
public static final String IPV6_HEXCOMPRESSED_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)\\z";
public static final String IPV6_REGEX = "\\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\\z";

Got these from some blog. Someone good w/ regexes 4 should be able to come up with a single 3 regex for all IPv6 address types. Actually, I 2 guess you could have a single regex that 1 matches both IPv4 and IPv6.

Score: 6

Another good option for processing IPs is 5 to use Java's classes Inet4Address and Inet6Address, which can be 4 useful in a number of ways, one of which 3 is to determine the validity of the IP address.

I 2 know this doesn't answer the question directly, but 1 just thought it's worth mentioning.

Score: 3

Here's a regex to match IPv4 addresses:


You'll 2 need to escape the backslashes when you 1 specify it as a string literal in Java:

Score: 1
package com.capgemini.basics;

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

public class Main {
    private static Pattern VALID_IPV4_PATTERN = null;
    private static Pattern VALID_IPV6_PATTERN1 = null;
    private static Pattern VALID_IPV6_PATTERN2 = null;
    private static final String ipv4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
    private static final String ipv6Pattern1 = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";
    private static final String ipv6Pattern2 = "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$";

    static {
        try {
            VALID_IPV4_PATTERN = Pattern.compile(ipv4Pattern, Pattern.CASE_INSENSITIVE);
            VALID_IPV6_PATTERN1 = Pattern.compile(ipv6Pattern1, Pattern.CASE_INSENSITIVE);
            VALID_IPV6_PATTERN2 = Pattern.compile(ipv6Pattern2, Pattern.CASE_INSENSITIVE);
        } catch (PatternSyntaxException e) {

    public static List<String> validateAddresses(List<String> ipAddress) {
        final List<String> validity= new ArrayList<String>();
        int len = ipAddress.size();
        for(int i=0; i<len; i++){
            Matcher m1 = Main.VALID_IPV4_PATTERN.matcher(ipAddress.get(i));
            Matcher m12 = Main.VALID_IPV6_PATTERN1.matcher(ipAddress.get(i));
            Matcher m22 = Main.VALID_IPV6_PATTERN2.matcher(ipAddress.get(i));
            if (m1.matches()) {
            else if(m12.matches() || m22.matches()){
        return validity;


    public static void main(String[] args)
        final List<String> IPAddress = new ArrayList<String>();
        //Test Case 0

        //Test Case 1

        //Test Case 2

        final List<String> result = validateAddresses(IPAddress);

        for (int i=0; i<result.size(); i++)
            System.out.println(result.get(i)+" ");




Score: 0

The regex allows the use of leading zeros 3 in the IPv4 parts.

Some Unix and Mac distros 2 convert those segments into octals.

I suggest 1 using 25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d as an IPv4 segment.

Score: 0

Regexes for ipv6 can get really tricky when 7 you consider addresses with embedded ipv4 6 and addresses that are compressed.

The open-source IPAddress Java library will 5 validate all standard representations of 4 IPv6 and IPv4 and also supports prefix-length 3 (and validation of such). Disclaimer: I 2 am the project manager of that library.

Code 1 example:

try {
    IPAddressString str = new IPAddressString("::1");
    IPAddress addr = str.toAddress();
} catch(AddressStringException e) {
    //e.getMessage has validation error

More Related questions