Regular Expressions That I Usually Use





5.00/5 (1 vote)
Regular expressions that I usually use
Here are some of the regular expressions that I regularly use.
\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:#@%/;$()~_?\+-=\\\.&]*)