Wednesday, March 23, 2011

REGEX Patterns Collection

This is an onoging collection of REGEX Matching Patterns that I have collected/created over the years.  It is not an exhaustive list, just what I have had a reason to use in REGEX validation/search/replace.

TenDigitPhoneNumberPattern
/// 
/// Tested and working with 
///  4631189780
///  463-118-9780
///  (579).293-3763
///  (560) 879.3837
/// 
public const string TenDigitPhoneNumberPattern = 
 "^\\(?([1-9]\\d{2})\\)?\\D*?([1-9]\\d{2})\\D*?(\\d{4})$";

NorthAmericanPhoneNumberPattern
/// 
/// 
/// Match a North American phone number 
/// with an optional area code and an optional "-" 
/// character to be used in the phone number 
/// and no extension
/// 
public const string NorthAmericanPhoneNumberPattern = 
 "^(\\(?[0-9]{3}\\)?)?\\-?[0-9]{3}\\-?[0-9]{4}$";

EmailAddressPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string EmailAddressPattern = 
 "\\A[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}\\z";

ShortDatePattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string ShortDatePattern = 
 "^([0-9]{1,2})/([0-9]{1,2})/([0-9]{4,4})$";

UppercaseAndLowercaseAlphaPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string UppercaseAndLowercaseAlphaPattern = 
 "[^a-zA-Z]";

UppercaseAlphaPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string UppercaseAlphaPattern = 
 "^[A-Z]*$";

AlphaNumericPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string AlphaNumericPattern = 
 "[^a-zA-Z0-9]";

WebSiteAddressPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string WebSiteAddressPattern = 
 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";

SocialSecurityNumberPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string SocialSecurityNumberPattern = 
 "^((?!000)([0-6]\\d{2}|[0-7]{2}[0-2]))-((?!00)\\d{2})-((?!0000)\\d{4})$";

NotIntegerPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string NotIntegerPattern = 
 "[^0-9-]";

IntegerPattern
/// 
/// 
/// Regex.IsMatch("-123", IntegerPattern);  //TRUE
/// Regex.IsMatch("-1 23", IntegerPattern);  //FALSE
/// Regex.IsMatch("+123", IntegerPattern);  // FALSE
/// Regex.IsMatch("   123", IntegerPattern);  //FALSE
/// Regex.IsMatch("000123", IntegerPattern);  //TRUE
/// Regex.IsMatch("123.123", IntegerPattern);  //FALSE
/// Regex.IsMatch("123-", IntegerPattern);  // FALSE
/// Regex.IsMatch("aaa", IntegerPattern);  // FALSE
/// Regex.IsMatch("$123", IntegerPattern);  //FALSE
/// 
public const string IntegerPattern = 
 "^-[0-9]+$|^[0-9]+$";

NotWholeNumberPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string NotWholeNumberPattern = 
 "[^0-9]";

NatualNumberPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string NatualNumberPattern = 
 "0*[1-9][0-9]*";

NotPositiveNumberPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string NotPositiveNumberPattern = 
 "[^0-9.]";

PositiveNumberPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string PositiveNumberPattern = 
 "^[.][0-9]+$|[0-9]*[.]*[0-9]+$";

TwoDotPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string TwoDotPattern = 
 "[0-9]*[.][0-9]*[.][0-9]*";

TwoMinusPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string TwoMinusPattern = 
 "[0-9]*[-][0-9]*[-][0-9]*";

ValidNumberPattern
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string ValidNumberPattern = 
 "(^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$)|(^([-]|[0-9])[0-9]*$)";

ValidZipCode
/// 
/// 
/// Tested. matches with with 
///  00000
///  09090-0000
///  000000000
/// 
public const string ValidZipCode = 
 "^\\d{5}$|^\\d{5}-\\d{4}$|^\\d{9}$";

ValidZipCodePlus4
/// 
/// 
/// Tested. matches with "0000"
/// 
public const string ValidZipCodePlus4 = 
 "^\\d{4}$";

GrabHTMLTagPatternStringFormat
/// 
/// 
/// Tested and working with --- (Needs examples)
/// 
public const string GrabHTMLTagPatternStringFormat = 
 @"<{0}\b[^>]*>(.*?)";

No comments:

Post a Comment