Regular Expression Snippets

US State

            ^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]))$

        

Zip Code

Matches 80523 and 80523-1000

            ^d{5}([-]d{4})?$

        

Campus Delivery Code

Matches four digits

            ^d{4}$

        

Phone Number

Matches phone numbers with or with out extensions, (555) 555-555 and (555) 555-5555 123

            ^+?(?d+)?(s|-|.)?d{1,3}(s|-|.)?d{4}[s]*[d]*$

        

Email

Matches an email address

            ([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})

        

Website URL

Matches an URL (loosely, URL’s are hard to predict)

            ((https?):((//)|(\\))+wd:#@%/;$()~_?+-*)  

        

Another that follows RFC Guidelines

            (([w]+:)?//)?(([dw]|%[a-fA-fd]{2,2})+(:([dw]|%[a-fA-fd]{2,2})+)?@)?([dw][-dw]{0,253}[dw].)+[w]{2,4}(:[d]+)?(/([-+_~.dw]|%[a-fA-fd]{2,2})*)*(?(&?([-+_~.dw]|%[a-fA-fd]{2,2})=?)*)?

        

FTP URL

Matches an ftp address

            ((ftp?):((//)|(\\))+wd:#@%/;$()~_?+-*)

        

CSU ID

Matches a valid CSU ID

            [8]d{8}

        

Image Validation (eg, using FileUpload control)

Matches a valid jpg, gif, or png file

            ^.+.((jpg)|(JPG)|(gif)|(GIF)|(jpeg)|(JPEG)|(png)|(PNG))$

        

GPA

Matches a valid GPA in US format (0.0 – 4.0)

            ^[0]|[0-3].(d?d?)|[4].[0]$

        

Tools

Basic HTML Tag Stripper:

            /// <summary>

/// Strip all html tags, even the clean ones

/// </summary>

/// <param name="text"></param>

/// <returns></returns>

public static string stripHTML(string text)

{

return Regex.Replace(text, @"<(.|n)*?>", string.Empty);

}

        

“Dirty” Word HTML (C# example):

            /// <summary>

/// Strip dirty Word HTML

/// </summary>

/// <param name="text"></param>

/// <returns></returns>

public static string stripWordHTML(string text)

{

////industrial grade word html cleaner

//courtesy of http://www.codinghorror.com/blog/2006/01/cleaning-words-nasty-html.html

StringCollection sc = new StringCollection();
// get rid of unnecessary tag spans (comments and title)
sc.Add(@”<!–(w|W)+?–>”);
sc.Add(@”<title>(w|W)+?</title>”);
// Get rid of classes and styles
sc.Add(@”s?class=w+”);
sc.Add(@”s+style='[^’]+'”);
// Get rid of unnecessary tags
sc.Add(
@”<(meta|link|/?o:|/?style|/?div|/?std|/?head|/?html|body|/?body|/?span|![)[^>]*?>”);
// Get rid of empty paragraph tags
sc.Add(@”(<[^>]+>)+ (</w+>)+”);
// remove bizarre v: element attached to <img> tag
sc.Add(@”s+v:w+=””[^””]+”””);
// remove extra lines
sc.Add(@”(nr){2,}”);
foreach (string s in sc)
{
text = Regex.Replace(text, s, “”, RegexOptions.IgnoreCase);
}

return text;
}