<p>Looking for a string to pass to String#matches(String) that will match IPv4, and another to match IPv6.</p>

[{'answer_id': 46168, 'author': 'Kevin Wong', 'author_id': 4792, 'author_profile': 'https://Stackoverflow.com/users/4792', 'pm_score': 4, 'selected': False, 'text': '<pre><code>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";\npublic 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";\npublic 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";\npublic 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";\npublic static final String IPV6_REGEX = "\\\\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\\\\z";\n</code></pre>\n\n<p>Got these from some blog. Someone good w/ regexes should be able to come up with a single regex for all IPv6 address types.  Actually, I guess you could have a single regex that matches both IPv4 and IPv6.</p>\n'}, {'answer_id': 46171, 'author': 'hasseg', 'author_id': 4111, 'author_profile': 'https://Stackoverflow.com/users/4111', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s a regex to match IPv4 addresses:</p>\n\n<pre><code>\\b(?:(?: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]?)\\b\n</code></pre>\n\n<p>You\'ll need to escape the backslashes when you specify it as a string literal in Java:</p>\n\n<pre><code>"\\\\b(?:(?: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]?)\\\\b"\n</code></pre>\n'}, {'answer_id': 1210965, 'author': 'Yuval', 'author_id': 2819, 'author_profile': 'https://Stackoverflow.com/users/2819', 'pm_score': 3, 'selected': False, 'text': '<p>Another good option for processing IPs is to use Java\'s classes <a href="http://java.sun.com/javase/6/docs/api/java/net/Inet4Address.html" rel="noreferrer">Inet4Address</a> and <a href="http://java.sun.com/javase/6/docs/api/java/net/Inet6Address.html" rel="noreferrer">Inet6Address</a>, which can be useful in a number of ways, one of which is to determine the validity of the IP address.</p>\n\n<p>I know this doesn\'t answer the question directly, but just thought it\'s worth mentioning.</p>\n'}, {'answer_id': 1573032, 'author': 'Aeron', 'author_id': 190680, 'author_profile': 'https://Stackoverflow.com/users/190680', 'pm_score': 0, 'selected': False, 'text': '<p>The regex allows the use of leading zeros in the IPv4 parts.</p>\n\n<p>Some Unix and Mac distros convert those segments into octals.</p>\n\n<p>I suggest using <code>25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d</code> as an IPv4 segment.</p>\n'}, {'answer_id': 42605542, 'author': 'Sean F', 'author_id': 6801443, 'author_profile': 'https://Stackoverflow.com/users/6801443', 'pm_score': 0, 'selected': False, 'text': '<p>Regexes for ipv6 can get really tricky when you consider addresses with embedded ipv4 and addresses that are compressed.</p>\n\n<p><a href="https://seancfoley.github.io/IPAddress/" rel="nofollow noreferrer">The open-source IPAddress Java library</a> will validate all standard representations of IPv6 and IPv4 and also supports prefix-length (and validation of such).  Disclaimer: I am the project manager of that library.</p>\n\n<p>Code example:</p>\n\n<pre><code>try {\n    IPAddressString str = new IPAddressString("::1");\n    IPAddress addr = str.toAddress();\n} catch(AddressStringException e) {\n    //e.getMessage has validation error\n}\n</code></pre>\n'}, {'answer_id': 60502882, 'author': 'Bodhiswatta Mukherjee', 'author_id': 12998124, 'author_profile': 'https://Stackoverflow.com/users/12998124', 'pm_score': 1, 'selected': False, 'text': '<pre><code>package com.capgemini.basics;\n\nimport java.util.*;\nimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\nimport java.util.regex.PatternSyntaxException;\n\npublic class Main {\n    private static Pattern VALID_IPV4_PATTERN = null;\n    private static Pattern VALID_IPV6_PATTERN1 = null;\n    private static Pattern VALID_IPV6_PATTERN2 = null;\n    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])";\n    private static final String ipv6Pattern1 = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";\n    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})*)?)$";\n\n\n    static {\n        try {\n            VALID_IPV4_PATTERN = Pattern.compile(ipv4Pattern, Pattern.CASE_INSENSITIVE);\n            VALID_IPV6_PATTERN1 = Pattern.compile(ipv6Pattern1, Pattern.CASE_INSENSITIVE);\n            VALID_IPV6_PATTERN2 = Pattern.compile(ipv6Pattern2, Pattern.CASE_INSENSITIVE);\n        } catch (PatternSyntaxException e) {\n            System.out.println("Neither");\n        }\n    }\n\n    public static List&lt;String&gt; validateAddresses(List&lt;String&gt; ipAddress) {\n        final List&lt;String&gt; validity= new ArrayList&lt;String&gt;();\n        int len = ipAddress.size();\n        for(int i=0; i&lt;len; i++){\n            Matcher m1 = Main.VALID_IPV4_PATTERN.matcher(ipAddress.get(i));\n            Matcher m12 = Main.VALID_IPV6_PATTERN1.matcher(ipAddress.get(i));\n            Matcher m22 = Main.VALID_IPV6_PATTERN2.matcher(ipAddress.get(i));\n            if (m1.matches()) {\n                validity.add("IPv4");\n            }\n            else if(m12.matches() || m22.matches()){\n                validity.add("IPv6");\n            }\n            else{\n                validity.add("Neither");\n            }\n        }\n        return validity;\n\n    }\n\n    public static void main(String[] args)\n    {\n        final List&lt;String&gt; IPAddress = new ArrayList&lt;String&gt;();\n        //Test Case 0\n        /*IPAddress.add("121.18.19.20");\n        IPAddress.add("0.12.12.34");\n        IPAddress.add("121.234.12.12");\n        IPAddress.add("23.45.12.56");\n        IPAddress.add("0.1.2.3");*/\n\n        //Test Case 1\n        /*IPAddress.add("2001:0db8:0000:0000:0000:ff00:0042:8329");\n        IPAddress.add("2001:0db8:0:0:0:ff00:42:8329");\n        IPAddress.add("::1");\n        IPAddress.add("2001:0db8::ff00:42:8329");\n        IPAddress.add("0000:0000:0000:0000:0000:0000:0000:0001");*/\n\n        //Test Case 2\n        /*IPAddress.add("000.012.234.23");\n        IPAddress.add("666.666.23.23");\n        IPAddress.add(".213.123.23.32");\n        IPAddress.add("23.45.22.32.");\n        IPAddress.add("272:2624:235e:3bc2:c46d:682:5d46:638g");\n        IPAddress.add("1:22:333:4444");*/\n\n        final List&lt;String&gt; result = validateAddresses(IPAddress);\n\n        for (int i=0; i&lt;result.size(); i++)\n            System.out.println(result.get(i)+" ");\n\n    }\n\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/46146', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4792/']