Is Valid URL by regex - CSharp System.Text.RegularExpressions

CSharp examples for System.Text.RegularExpressions:Match URL

Description

Is Valid URL by regex

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System;//from   ww w .  ja va 2 s  .  c o  m

public class Main{
        public static bool IsValidURL(string url)
      {
         // Allows HTTP and FTP URL's, domain name must start with alphanumeric and can contain a port
         // number followed by a path containing a standard path character and ending in common file
         // suffixies found in URL's and accounting for potential CGI GET data
         string regExPattern = @"^^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_=]*)?$";
         return MatchString(url, regExPattern);
      }
        // function designed to take a string and regular expression and return true if the regex
      // validates the string false if the string fails the regex.
      public static bool MatchString(string str, string regexstr)
      {
         str = str.Trim();
         Regex pattern = new Regex(regexstr);
         return pattern.IsMatch(str);
      }
}

Related Tutorials