Click here to Skip to main content
13,193,600 members (42,998 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.6K views
7 bookmarked
Posted 23 Mar 2010

Regular Expressions That I Usually Use

, 23 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Regular expressions that I usually use

Here are some of the regular expressions that I regularly use.

Email

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

British Date Format (dd/MM/yyyy)

^(?:(?:0?[1-9]|1\d|2[0-8])(\/|-)(?:0?[1-9]|1[0-2]))(\/|-)
(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^
(?:(?:31(\/|-)(?:0?[13578]|1[02]))|(?:(?:29|30)(\/|-)(?:0?[1,3-9]|1[0-2])))
(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(29(\/|-)0?2)(\/|-)
(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?
(?:0[48]|[2468][048]|[13579][26]))$

File Type Restriction (e.g *.doc, *.docx)

^.+\\.(([jJ][pP][eE][gG])|([jJ][pP][gG])|([gG][iI][fF])|([xX][lL][sS])|
([xX][lL][sS][xX])|([dD][oO][cC])|([dD][oO][cC][xX])|([tT][iI][fF][fF])|
([tT][xX][tT])|([pP][dD][fF])|([pP][pP][tT])|([pP][pP][tT][xX])|
([pP][pP][sS])|([pP][pP][sS][xX])|([pP][nN][gG])|([bB][mM][pP])|
([hH][tT][mM][lL])|([hH][tT][mM])|([rR][aA][rR])|([zZ][iI][pP])|
([rR][iI][fF])|([rR][sS][fF])|([rR][tT][mM])|([rR][pP][tT])|([rR][aA][zZ])|
([mM][dD][bB])|([sS][wW][fF])|([mM][sS][gG]))$

But to make changing the file types easier, I either put my file types on a web config or database and create a method to extract those and generate the regular expression. Here is how I do it.

public static string GetRegularExpression()
 {
//These variables you can store anywhere you want but to simplify it I added it here
 string sRAWAllowedString = "jpeg,jpg,gif,xls,xlsx,doc,docx,tiff,txt,pdf,
     ppt,pptx,pps,ppsx,png,bmp,html,htm,rar,zip,rif,rsf,rtm,rpt,raz,mdb,swf,msg";
 string sPrefix = @"^.+\.(";
 string sSuffix = ")$";
 string sIndividualString = "";
 string sFullString = "";
 char[] delimiterChars = { ',' };
 string text = "";
 string[] words = null;

 text = sRAWAllowedString;
 if (text != null && text != "")
 {
 words = text.Split(delimiterChars);
 foreach (string s in words)
 {
 sIndividualString = GenerateRegExpString(s);
 sFullString = sFullString + sIndividualString + "|";
 }

 }
 sFullString = sFullString.Substring(0, sFullString.Length - 1);
 sFullString = sPrefix + sFullString + sSuffix;

 return sFullString;
 }
 private static string GenerateRegExpString(string s)
 {
 int iStringLentgh = s.Length;
 string sNewStringS = "";
 string sNewStringB = "";
 string sNewString = "";
 string sNewWord = "";

 for (int i = 0; i < iStringLentgh; i++)
 {
 sNewStringS = s.Substring(i, 1).ToLower();
 sNewStringB = s.Substring(i, 1).ToUpper();
 sNewString = "[" + sNewStringS + sNewStringB + "]";
 sNewWord = sNewWord + sNewString;
 }
 sNewWord = "(" + sNewWord + ")";
 return sNewWord;
 }

Capturing Only Alphanumeric Characters

\W*

So how do I use that on codes, here is how I do it:

string sOriginalString = "Your Sample Text Here, 1234123!!!";
string sStrippedOutString = Regex.Replace(sOriginalString, @"\W*", "");

Website Addresses

((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171018.2 | Last Updated 23 Mar 2010
Article Copyright 2010 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid