3

I use this code to valid url

public static bool isUrl(string text)
        {
            Uri uriResult;
            return Uri.TryCreate(text, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
        }

but this code, not working when i send parameters like google.com, www.google.com

it only work when i use http like http://google.com

i need a code for c#, like Patterns.WEB_URL.matcher(text).matches(); in android

Hadi Akbarzadeh
  • 750
  • 10
  • 18

1 Answers1

2

I use this regex.

[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)

Here's the regex in action with this question's url: enter image description here

So if a match is found you got yourself a URL. And if in the future you need to get parts of it it can do that for you too.

Display Name
  • 942
  • 1
  • 10
  • 20
  • 1
    Interesting in this regard: https://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – Jens May 07 '16 at 21:45