TODO:
Have you ever been looking for handy RegExp's. I have grabbed a few I use from time to time and have put them below.
SOLUTION:
//Find first non-digit
Regex firstNonDigitIndexRegexp = new Regex(@"\D",RegexOptions.RightToLeft);
//Test if a string ends with a digit
Regex endsWithDigitRegexp = new Regex(@"\d$");
//Test if a string starts with a digit
Regex startsWithDigitRegexp = new Regex(@"^\d");
//Find the last digit in a string
Regex lastDigitIndexRegexp = new Regex(@"\d", RegexOptions.RightToLeft);
//Test if a string contains only digits
Regex numbersOnlyRegExp = new Regex(@"\D");
//Validate an email address
public bool ValidateEmail(string EmailAddress)
{
string patternToMatch = @"^([a-zA-Z0-9_\+\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
System.Text.RegularExpressions.Match match = Regex.Match(EmailAddress, patternToMatch, RegexOptions.IgnoreCase);
return match.Success;
}
// Validate a URL
public static bool IsUrlValid(string url)
{
//ensure we have a valid prefix
if(!Regex.IsMatch(url, @"^(http|https)\:(\\\\|//)", RegexOptions.IgnoreCase))
url = String.Concat("http://", url); //default to http
string pattern = @"^(http|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$";
Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return reg.IsMatch(url);
}
NOTES:
No notes apply on this topic.