Click here to Skip to main content
15,868,016 members
Articles / Web Development / ASP.NET
Article

Regular Expressions which are needed in daily coding...

Rate me:
Please Sign up or sign in to vote.
2.00/5 (21 votes)
21 Mar 2007GPL3 65.1K   19   19
Regular Expressions for all purpose..

Introduction

Guys , we all know that regular expressions are very useful for checking any kind of patterns.

Background

So we have to know how to write regular expressions for our patterns. But don't worry here i will provide some but very useful regular expressions which will be useful for our coding.

Using the code

just copy and paste Regular Expression from here to your code.
//
// this are the regular expressions for all purposes...
//For checking opening and closing tab of specific HTML tags..
<tt class="regex"><TAG\b[^>]*>(.*?)</TAG></tt>
//for any HTML tags starting and ending..
<tt class="regex"><([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1></tt>
//For deleting white spaces from text...
<tt class="regex">^[ \t]+|[ \t]+$</tt>
//For Checking IP address between 0..255...
<tt class="regex">\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.</tt><tt class="regex">(25[0-5]|2[0-4][0-9]|[01]?[0-9]</tt>
<tt class="regex">[0-9]?)\.</tt><tt class="regex">(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.</tt><tt class="regex">
<tt class="regex">(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b</tt><tt class="regex"><tt class="regex"><tt class="regex"> 
//For floating point numbers with sign...
<tt class="regex">[-+]?[0-9]*\.?[0-9]+</tt>
//for floating point numbers with exponents...
<tt class="regex">[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?</tt><tt class="regex"> 
//For email validation...
<tt class="regex">^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$</tt></tt></tt></tt></tt></tt>
<tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex">//or you can try for .Museum and etc..</tt></tt></tt></tt></tt></tt>
<tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex">^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$</tt></tt></tt></tt></tt></tt>
<tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex"><tt class="regex">
//Date format for yyyy-mm-dd betwwen 1900-01-01 and 2099-12-31
<tt class="regex">(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])</tt>
<tt class="regex">//Date format for mm/dd/yyyy betwwen 1900-01-01 and 2099-12-31 
<tt class="regex">(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d</tt><tt class="regex"> <tt class="regex"> 
//Date format for dd-mm-yyyy betwwen 1900-01-01 and 2099-12-31 
<tt class="regex">(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d</tt> 
//finding word in a string..
<tt class="regex">^.*\b(one|two|three)\b.*$   //one,two,three may be your words...</tt>
//for deleting duplicate lines from text file...
<tt class="regex">^(.*)(\r?\n\1)+$</tt></tt></tt></tt></tt></tt></tt></tt></tt>
//For checking digits..
^[0-9]
//for checking alphabets...
^[a-z]</tt>
<tt class="regex">//REgular Expression for some Credit cards validations...</tt>
<tt class="regex">
//"Visa"
^[4]([0-9]{15}$|[0-9]{12}$)
//"MasterCard"
^[5][1-5][0-9]{14}$ 

//"Discover" 
^6011-?\\d{4}-?\\d{4}-?\\d{4}$

//"Diners Club" 
(^30[0-5][0-9]{11}$)|(^(36|38)[0-9]{12}$)

//"American Express" 
^[34|37][0-9]{14}$</tt>
<tt class="regex">
//"enRoute"
^(2014|2149)[0-9]{11}$

//"JCB" 
^3[0-9]{15}$)|(^(2131|1800)[0-9]{11}$)</tt>

Points of Interest

I like to Share my knowledge with you, because i am also one of you, taht if i don't know anything then i come to you...so keep excahnge of knowledge...

History

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
India India
Chirag Patel, a Programmer Analyst in a well known IT company working on .NET Technologies since last 2 years. He is interested in Pure business logic and fuzzy logic. his area of interest is in C#.NET, VB.NET and MSSQL 2005.

catch me on: http://groups.google.com/group/codewar

Comments and Discussions

 
NewsThere's a new version of the RegEx Tester Tool ! Pin
Pablo Osés1-Mar-08 23:21
Pablo Osés1-Mar-08 23:21 
GeneralFormatting issue Pin
Pete O'Hanlon22-Mar-07 23:32
subeditorPete O'Hanlon22-Mar-07 23:32 
Hi - could you please sort out the formatting from the Points of interest section on? The font style has changed here and was a little bit distracting for me - I really don't like serif fonts on web pages.

Apart from that, don't worry about the content. Maybe you could keep this as an area to expand whenever you find new and useful regular expressions.

Deja View - the feeling that you've seen this post before.

GeneralDon't be discouraged Pin
Bradml22-Mar-07 22:46
Bradml22-Mar-07 22:46 
QuestionWhat on earth? Pin
NormDroid21-Mar-07 21:57
professionalNormDroid21-Mar-07 21:57 
AnswerRe: What on earth? Pin
purplepangolin22-Mar-07 4:44
purplepangolin22-Mar-07 4:44 
GeneralRe: What on earth? Pin
NormDroid22-Mar-07 4:50
professionalNormDroid22-Mar-07 4:50 
GeneralRe: What on earth? Pin
purplepangolin22-Mar-07 5:04
purplepangolin22-Mar-07 5:04 
GeneralRe: What on earth? Pin
NormDroid22-Mar-07 5:48
professionalNormDroid22-Mar-07 5:48 
GeneralRe: What on earth? Pin
purplepangolin22-Mar-07 6:17
purplepangolin22-Mar-07 6:17 
GeneralRe: What on earth? Pin
JustChiragPatel22-Mar-07 17:31
JustChiragPatel22-Mar-07 17:31 
GeneralRe: What on earth? Pin
NormDroid22-Mar-07 21:42
professionalNormDroid22-Mar-07 21:42 
GeneralRe: What on earth? Pin
NormDroid22-Mar-07 21:41
professionalNormDroid22-Mar-07 21:41 
AnswerRe: What on earth? Pin
ednrgc22-Mar-07 5:20
ednrgc22-Mar-07 5:20 
GeneralRe: What on earth? Pin
NormDroid22-Mar-07 5:49
professionalNormDroid22-Mar-07 5:49 
AnswerRe: What on earth? Pin
JustChiragPatel22-Mar-07 17:30
JustChiragPatel22-Mar-07 17:30 
GeneralRe: What on earth? Pin
NormDroid22-Mar-07 21:40
professionalNormDroid22-Mar-07 21:40 
GeneralRe: What on earth? Pin
#realJSOP23-Mar-07 0:25
mve#realJSOP23-Mar-07 0:25 
GeneralRe: What on earth? Pin
NormDroid23-Mar-07 1:19
professionalNormDroid23-Mar-07 1:19 
GeneralRe: What on earth? Pin
#realJSOP23-Mar-07 23:35
mve#realJSOP23-Mar-07 23:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.