Another question with regex url prefix (which will be used in C #)

I have seen many regular expressions for validating URLs. In my case, I want Url to be simpler, so the regex should be tougher:

Valid url prefixes are as follows:

  • HTTP [s]: [. WWW] [?] // addressOrIp [.something] /PageName.aspx

The prefix is โ€‹โ€‹described here. I will add ?x=a&y=b&z=clater. I just want to check if the web page is alive before accessing it, but before that I want to make sure that it is configured correctly. I want to refer to the conditions bad urland host is downin different ways, though, if in doubt, I'd rather give a message host is down, because that is the ultimate test in any case. Hope this makes sense. I suppose I'm trying to say that the regular expression should not be too aggressive, I just want it to cover 95% of cases.

This is C # -centric, so Perl regex extensions don't help me; let them adhere to the lowest common denominator.

Thank!

+3
source share
2 answers

Uri:

Uri uri;

if (!Uri.TryCreate(str, UriKind.Absolute, out uri))
    //Bad bad bad!!!
if (uri.Scheme != "http" && uri.Scheme != "https")
    //Bad bad bad!!!
if (uri.Host.IndexOf('.') <0)
    //Bad bad bad!!!
+4
+1

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


All Articles