<p>How does one go about authoring a <em><strong>Regular Expression</strong></em> that matches against all strings that are valid URIs, while failing to match against all strings that are invalid URIs?</p>
<p>To be specific about what I am referencing when I say URI, I have added a link below for the most current URI RFC standard. It defines the entity that I want to validate using a regular expression.</p>
<p>I don't need it to be able to parse the URI. I just need a regular expression for validating.</p>
<p>The <strong>.Net Regular Expression Format</strong> is preferred. (.Net V1.1)</p>
<br>
<h4>My Current Solution:</h4>
<pre><code>^([a-zA-Z0-9+.-]+):(//([a-zA-Z0-9-._~!$&amp;'()*+,;=:]*)@)?([a-zA-Z0-9-._~!$&amp;'()*+,;=]+)(:(\\d*))?(/?[a-zA-Z0-9-._~!$&amp;'()*+,;=:/]+)?(\\?[a-zA-Z0-9-._~!$&amp;'()*+,;=:/?@]+)?(#[a-zA-Z0-9-._~!$&amp;'()*+,;=:/?@]+)?$(:(\\d*))?(/?[a-zA-Z0-9-._~!$&amp;'()*+,;=:/]+)?(\?[a-zA-Z0-9-._~!$&amp;'()*+,;=:/?@]+)?(\#[a-zA-Z0-9-._~!$&amp;'()*+,;=:/?@]+)?$
</code></pre>

[{'answer_id': 30852, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 1, 'selected': False, 'text': '<p>Are there some specific URIs you care about or are you trying to find a single regex that validates <a href="http://rfc.net/std0066.html" rel="nofollow noreferrer">STD66</a>?</p>\n\n<p>I was going to point you to <a href="http://www.unix.com.ua/rfc/std0066.html#p50" rel="nofollow noreferrer">this regex</a> for parsing a URI.  You could then, in theory, check to see if all of the elements you care about are there. </p>\n\n<p>But I think <a href="https://stackoverflow.com/questions/30847#30858">bdukes</a> answer is better.</p>\n'}, {'answer_id': 30858, 'author': 'bdukes', 'author_id': 2688, 'author_profile': 'https://Stackoverflow.com/users/2688', 'pm_score': 5, 'selected': False, 'text': '<p>Does <a href="http://msdn.microsoft.com/en-us/library/system.uri.iswellformeduristring.aspx" rel="noreferrer">Uri.IsWellFormedUriString</a> work for you?</p>\n'}, {'answer_id': 30910, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 5, 'selected': True, 'text': '<p>This site looks promising: <a href="http://snipplr.com/view/6889/regular-expressions-for-uri-validationparsing/" rel="noreferrer">http://snipplr.com/view/6889/regular-expressions-for-uri-validationparsing/</a></p>\n\n<p>They propose following regex: </p>\n\n<pre><code>/^([a-z0-9+.-]+):(?://(?:((?:[a-z0-9-._~!$&amp;\'()*+,;=:]|%[0-9A-F]{2})*)@)?((?:[a-z0-9-._~!$&amp;\'()*+,;=]|%[0-9A-F]{2})*)(?::(\\d*))?(/(?:[a-z0-9-._~!$&amp;\'()*+,;=:@/]|%[0-9A-F]{2})*)?|(/?(?:[a-z0-9-._~!$&amp;\'()*+,;=:@]|%[0-9A-F]{2})+(?:[a-z0-9-._~!$&amp;\'()*+,;=:@/]|%[0-9A-F]{2})*)?)(?:\\?((?:[a-z0-9-._~!$&amp;\'()*+,;=:/?@]|%[0-9A-F]{2})*))?(?:#((?:[a-z0-9-._~!$&amp;\'()*+,;=:/?@]|%[0-9A-F]{2})*))?$/i\n</code></pre>\n'}, {'answer_id': 10575584, 'author': 'jcsahnwaldt Reinstate Monica', 'author_id': 131160, 'author_profile': 'https://Stackoverflow.com/users/131160', 'pm_score': 4, 'selected': False, 'text': '<p>The <a href="https://www.rfc-editor.org/rfc/rfc3986#appendix-B" rel="nofollow noreferrer">URI specification says</a>:</p>\n<blockquote>\n<p><em>The following line is the regular expression for breaking-down a well-formed URI reference into its components.</em></p>\n<p><code>^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?</code></p>\n</blockquote>\n<p>(I guess that\'s the same regex as in the STD66 link given in another answer.)</p>\n<p>But <em>breaking-down</em> is not <em>validating</em>. To correctly validate a URI, one would have to translate the <a href="https://www.rfc-editor.org/rfc/rfc3986#appendix-A" rel="nofollow noreferrer">BNF for URIs</a> to a regex. While some BNFs <em>cannot</em> be expressed as regular expressions, I think with this one it <em>could</em> be done. But it shouldn\'t be done - it would be a huge mess. It\'s better to use a library function.</p>\n'}, {'answer_id': 17813893, 'author': 'papercowboy', 'author_id': 641934, 'author_profile': 'https://Stackoverflow.com/users/641934', 'pm_score': 3, 'selected': False, 'text': '<p>The best and most definitive guide to this I have found is here: <a href="http://jmrware.com/articles/2009/uri_regexp/URI_regex.html#uri-40" rel="noreferrer">http://jmrware.com/articles/2009/uri_regexp/URI_regex.html</a> (In answer to your question, see the <strong>URI</strong> table entry)</p>\n\n<blockquote>\n  <p>All of these rules from RFC3986 are reproduced in Table 2 along with a regular expression implementation for each rule.</p>\n</blockquote>\n\n<p>A javascript implementation of this is available here: <a href="https://github.com/jhermsmeier/uri.regex" rel="noreferrer">https://github.com/jhermsmeier/uri.regex</a></p>\n\n<p>For reference, the URI regex is repeated below:</p>\n\n<pre><code># RFC-3986 URI component:  URI\n[A-Za-z][A-Za-z0-9+\\-.]* :                                      # scheme ":"\n(?: //                                                          # hier-part\n  (?: (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:]|%[0-9A-Fa-f]{2})* @)?\n  (?:\n    \\[\n    (?:\n      (?:\n        (?:                                                    (?:[0-9A-Fa-f]{1,4}:)    {6}\n        |                                                   :: (?:[0-9A-Fa-f]{1,4}:)    {5}\n        | (?:                            [0-9A-Fa-f]{1,4})? :: (?:[0-9A-Fa-f]{1,4}:)    {4}\n        | (?: (?:[0-9A-Fa-f]{1,4}:){0,1} [0-9A-Fa-f]{1,4})? :: (?:[0-9A-Fa-f]{1,4}:)    {3}\n        | (?: (?:[0-9A-Fa-f]{1,4}:){0,2} [0-9A-Fa-f]{1,4})? :: (?:[0-9A-Fa-f]{1,4}:)    {2}\n        | (?: (?:[0-9A-Fa-f]{1,4}:){0,3} [0-9A-Fa-f]{1,4})? ::    [0-9A-Fa-f]{1,4}:\n        | (?: (?:[0-9A-Fa-f]{1,4}:){0,4} [0-9A-Fa-f]{1,4})? ::\n        ) (?:\n            [0-9A-Fa-f]{1,4} : [0-9A-Fa-f]{1,4}\n          | (?: (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\.){3}\n                (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n          )\n      |   (?: (?:[0-9A-Fa-f]{1,4}:){0,5} [0-9A-Fa-f]{1,4})? ::    [0-9A-Fa-f]{1,4}\n      |   (?: (?:[0-9A-Fa-f]{1,4}:){0,6} [0-9A-Fa-f]{1,4})? ::\n      )\n    | [Vv][0-9A-Fa-f]+\\.[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:]+\n    )\n    \\]\n  | (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}\n       (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\n  | (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=]|%[0-9A-Fa-f]{2})*\n  )\n  (?: : [0-9]* )?\n  (?:/ (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )*\n| /\n  (?:    (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:@]|%[0-9A-Fa-f]{2})+\n    (?:/ (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )*\n  )?\n|        (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:@]|%[0-9A-Fa-f]{2})+\n    (?:/ (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )*\n|\n)\n(?:\\? (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )?   # [ "?" query ]\n(?:\\# (?:[A-Za-z0-9\\-._~!$&amp;\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )?   # [ "#" fragment ]\n</code></pre>\n'}, {'answer_id': 45690571, 'author': 'Lostfields', 'author_id': 6330937, 'author_profile': 'https://Stackoverflow.com/users/6330937', 'pm_score': 3, 'selected': False, 'text': '<p>The best regex I came up with according to RFC 3986 (<a href="https://www.rfc-editor.org/rfc/rfc3986" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc3986</a>) was the following:</p>\n<p><a href="https://i.stack.imgur.com/xieJV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xieJV.png" alt="Flow diagram of regex using https://regexper.com" /></a></p>\n<pre><code>// named groups\n/^(?&lt;scheme&gt;[a-z][a-z0-9+.-]+):(?&lt;authority&gt;\\/\\/(?&lt;user&gt;[^@]+@)?(?&lt;host&gt;[a-z0-9.\\-_~]+)(?&lt;port&gt;:\\d+)?)?(?&lt;path&gt;(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@])+(?:\\/(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@])*)*|(?:\\/(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@])+)*)?(?&lt;query&gt;\\?(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@]|[/?])+)?(?&lt;fragment&gt;\\#(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@]|[/?])+)?$/i\n\n// unnamed groups\n/^([a-z][a-z0-9+.-]+):(\\/\\/([^@]+@)?([a-z0-9.\\-_~]+)(:\\d+)?)?((?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@])+(?:\\/(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@])*)*|(?:\\/(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@])+)*)?(\\?(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@]|[/?])+)?(\\#(?:[a-z0-9-._~]|%[a-f0-9]|[!$&amp;\'()*+,;=:@]|[/?])+)?$/i\n</code></pre>\n<p>capture groups</p>\n<ol>\n<li>scheme</li>\n<li>authority</li>\n<li>userinfo</li>\n<li>host</li>\n<li>port</li>\n<li>path</li>\n<li>query</li>\n<li>fragment</li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/30847', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/80/']