Problem with validating URL with regular expressions in C #

0

I have a problem I have this regular expression:

Regex Valida = new Regex(@"^(http|ftp|https|www)://([\w+?\.\w+])+([a-zA-
Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\/\?\.\:\;\'\,]*)?$", 
RegexOptions.IgnoreCase);

The problem is that it does not validate URLs that start with WWW .

What is missing for the expression to validate URLs such as www.google.com and google.com ?

    
asked by Breynner Tesillo 13.09.2018 в 19:23
source

3 answers

0

Responding to your question (What am I missing?), the regular expression you put in should find what you ask for.

You can check here .

I do not know if what you want to say is that you need to find the same thing but making the protocol (http / https / ...) optional.

In this case, you can achieve it by putting it in a group and making it optional with ? . So ^(?:(http|ftp|https|www)://)?

This would work as well for google.com and www.google.com with nothing in front.

Anyway, validating a url by means of a regular expression can be more complicated than it seems, since it has many cases in which you have probably not thought about.

It is best to use a regular expression that is accepted as correct and widely tested.

In this post from SO in English some solutions are discussed. Even for C # there is a solution without using regular expressions.

    
answered by 20.09.2018 / 18:25
source
1

Another option using Regex may be this

string regex = @"(((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).?)(:\d*)?)(/((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|@)+(/(([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|@)))?)?(\?((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|@)|[\uE000-\uF8FF]|/|\?)*)?(#((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|@)|/|\?)*)?$";
string url = "https://www.google.com.ar";
bool isValid = (Regex.IsMatch(url, regex));

Source

    
answered by 13.09.2018 в 20:18
0

You can directly use the following C # method to validate URLs:

Uri.IsWellFormedUriString(URL_a_Validar, UriKind.RelativeOrAbsolute)
    
answered by 13.09.2018 в 20:12