title: Regex for validating URL with brackets in Swift
body: <p>In my app, I need to validate the URL that the user inputs into the textField. Valid URL might have brackets with number at the end, for example, &quot;https://stackoverflow.com(24)&quot;. The URL without brackets, for example, &quot;https://stackoverflow.com&quot;, is also valid.</p>
  <p>I use this code for validating URL without brackets, and it works:</p>
  <pre><code>extension String {
      
      func validateUrl() -&gt; Bool {
          let urlRegEx =
              &quot;^(http|https|ftp)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&amp;amp;%\\$\\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|ru|kz|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\?\\'\\\\\\+&amp;amp;%\\$#\\=~_\\-]+))*$&quot;
          return NSPredicate(format: &quot;SELF MATCHES %@&quot;, urlRegEx).evaluate(with: self)
      }
      
  }
  </code></pre>
  <p>However, it doesn't validate URLs with brackets. What should I end at the end of this regex to make it work for both types of URLs? Any help is appreciated!</p>