Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Regular Expressions that I usually use

, 23 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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])$&#124

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/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

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