Regular regex for checking web addresses?

I am looking for a regex that checks simple website addresses, i.e.

I need it for the contact information, the "Website" field, and then when the user clicks on it, IE opens, it does not have to be strict , I just do not want the user to enter "I like milk" or "google" and etc.

I thought that instead, I am embarrassed, making my own attempt to find an exception, why I do not learn from the experience of the community, anyone who has a good regular expression or link, please write.

Many thanks.

+4
source share
6 answers

From RFC 3986, Uniform Resource Identifiers (URIs): General Syntax , Appendix B (p. 50):

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? 

If the URI matches this regular expression, it is well-formed. Match groups give you various snippets that:

 scheme = $2 authority = $4 path = $5 query = $7 fragment = $9 
+7
source
 https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)? 

excerpt from http://snipplr.com/view/2371/regex-regular-expression-to-match-a-url/


  (https?://)?([-\w\.]+)+(:\d+)? 

review the proposal, but I think that people should better follow the prompt and determine the answer themselves. in any case, even copy / paste, people should know what they are doing.

+1
source

I also mentioned RFC 3986, but it's too general, as it is designed to map relative URLs. Obviously, in the case of OP, we want absolute public URLs.

Something like ^(https?://)?(?:[\w.]+)\.(?:[\w:.]+) seems more realistic. I intentionally exclude sites requiring a username / password pair, but accept the port number.
Such an expression will be broken when Unicode URLs are common ...

[EDIT] I accepted any scheme, I have to limit more, I suppose, I don’t need to accept ftp addresses or bzr + ssl: // for an open case ...

0
source

Checking or finding URLs is not as easy as it sounds. This is a blog post that goes deeper into this topic:

http://www.blog.activa.be/2008/10/30/ExtractingURLsNotPerfectButQuotgoodEnoughquot.aspx

as well as:

http://www.codinghorror.com/blog/archives/001181.html

0
source

One parameter without using a regular expression, but address should start with a protocol like http:// :

 static bool validateAddress(string address) { Uri valid = null; return address.StartsWith("http") && Uri.TryCreate(address, UriKind.Absolute, out valid); } 
0
source

.. personally .. I usually watch the cover of regexp AddBytes

0
source

Source: https://habr.com/ru/post/1298967/


All Articles