<p>How can I determine if a string is an IP address? Either <em>IPv4</em> or <em>IPv6</em>?</p>

<p>What is the least and most number of characters?</p>

<p>I assume this would be a <strong>regex</strong> answer.</p>

[{'answer_id': 42359, 'author': 'Bill the Lizard', 'author_id': 1288, 'author_profile': 'https://Stackoverflow.com/users/1288', 'pm_score': 2, 'selected': False, 'text': '<p>For IPv4 you can use this regular expression.</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>It looks quite complex but it works by limiting each quad to the numbers 0-255.</p>\n'}, {'answer_id': 42376, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': 0, 'selected': False, 'text': "<p>Since half of that regex handles the fact that the last segment doesn't have a period at the end, you could cut it in half if you tack a '.' to the end of your possible IP address.</p>\n\n<p>Something like this:</p>\n\n<pre><code>bool IsValidIPAddress(string possibleIP){\n  CrazyRegex = \\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){4}\\b\n  return Regex.Match(possibleIP+'.', CrazyRegex)\n}\n</code></pre>\n"}, {'answer_id': 42384, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 0, 'selected': False, 'text': '<p>@unsliced that is correct however it will of course depend on implementation, if you are parsing an IP from a user visiting your site then your are fine to use regex as it SHOULD be in x.x.x.x format.</p>\n\n<p>For IPv6 you could use this</p>\n\n<pre><code>[A-F0-9]{0,4}:[A-F0-9]{0,4}:[A-F0-9]{0,4}:[A-F0-9]{0,4}:[A-F0-9]{0,4}:[A-F0-9]{0,4}:[A-F0-9]{0,4}:[A-F0-9]{0,4}\n</code></pre>\n\n<p>however it does not catch everything because with IPv6 it is much more complicated, acording to wikipedia all of the following examples are technicaly correct however the regex above will only catch the ones with a *</p>\n\n<pre><code>2001:0db8:0000:0000:0000:0000:1428:57ab*\n2001:0db8:0000:0000:0000::1428:57ab*\n2001:0db8:0:0:0:0:1428:57ab*\n2001:0db8:0:0::1428:57ab\n2001:0db8::1428:57ab\n2001:db8::1428:57ab\n</code></pre>\n'}, {'answer_id': 42393, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': 3, 'selected': False, 'text': '<p>In .NET there\'s an IPAddress type which has a handy method <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.tryparse.aspx" rel="noreferrer">TryParse</a>.</p>\n\n<p>Example: </p>\n\n<pre><code>if(System.Net.IPAddress.TryParse(PossibleIPAddress, validatedIPAddress)){\n    //validatedIPAddress is good\n}\n\n// or more simply:\nbool IsValidIPAddress(string possibleIP){\n    return System.Net.IPAddress.TryParse(PossibleIPAddress, null)\n}\n</code></pre>\n'}, {'answer_id': 42413, 'author': 'Niniki', 'author_id': 4155, 'author_profile': 'https://Stackoverflow.com/users/4155', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve done this before, but I like Raymond Chen\'s post at:</p>\n\n<p><a href="http://blogs.msdn.com/oldnewthing/archive/2006/05/22/603788.aspx" rel="noreferrer">http://blogs.msdn.com/oldnewthing/archive/2006/05/22/603788.aspx</a></p>\n\n<p>Where he basically advocates using regexes for what they\'re good at: parsing out the tokens.  Then evaluate the results.  His example:</p>\n\n<pre><code>function isDottedIPv4(s)\n{\n var match = s.match(/^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$/);\n return match != null &amp;&amp;\n        match[1] &lt;= 255 &amp;&amp; match[2] &lt;= 255 &amp;&amp;\n        match[3] &lt;= 255 &amp;&amp; match[4] &lt;= 255;\n}\n</code></pre>\n\n<p>It\'s much easier to look at that and grok what it\'s supposed to be doing.</p>\n'}, {'answer_id': 42584, 'author': 'Dough', 'author_id': 4451, 'author_profile': 'https://Stackoverflow.com/users/4451', 'pm_score': -1, 'selected': False, 'text': "<p>IPv4 becomes: <code>/\\d\\d?\\d?.\\d\\d?\\d?.\\d\\d?\\d?.\\d\\d?\\d?/</code></p>\n\n<p>I'm not sure about the IPv6 rules.</p>\n"}]
['https://Stackoverflow.com/questions/42345', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2644/']