C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Regular Expression Samples


Posted Date: 03 Oct 2008    Resource Type: Code Snippets    Category: Regular Expressions

Posted By: Dharmaraj       Member Level: Diamond
Rating:     Points: 10



This code shows how to use regular expression in c#

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RegularExpression
{
class Program
{
static void Main(string[] args)
{
Validation valid = new Validation();
bool flag = valid.IsAlphaNumeric("10A");
Console.WriteLine(flag);
}
}
//Class for the Validation
class Validation
{
//Function to test for the positive integer
public bool IsNaturalNumber(string strNumber)
{
Regex NotNatural = new Regex("[^0-9]");
Regex Natural = new Regex("0*[1-9][0-9]*");
return !NotNatural.IsMatch(strNumber) && Natural.IsMatch(strNumber);
}
//Function for test the natural numbes with zero
public bool IsWholeNumber(string strNumber)
{
Regex whole = new Regex("[^0-9]");
return !whole.IsMatch(strNumber);
}
//Function to check for a given string is a number or not
public bool IsNumber(string strNumber)
{
Regex NotNumberPattern = new Regex("[^0-9]");
Regex TwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex TwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
string strValidRealPattern = "^([-][.][-.]|[0-9])[0-9]*[.]*[0-9]+$";
string validIntegerPattern = "^([-][0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + validIntegerPattern + ")");
return !objNumberPattern.IsMatch(strNumber) &&
!TwoDotPattern.IsMatch(strNumber) &&
!TwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);

}
//Function to check for the alphabets
public bool IsAlpha(string strCheck)
{
Regex objAlphaPattern = new Regex("[^a-zA-Z]");
return !objAlphaPattern.IsMatch(strCheck);
}
//Function to check for the alphanumeric
public bool IsAlphaNumeric(string strCheck)
{
Regex objAlphaNumeric = new Regex("[^a-zA-Z0-9]");
return !objAlphaNumeric.IsMatch(strCheck);
}
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Regular Expression  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Restricting Date Values with Regular Expression validator
Previous Resource: Regular Expression for DD/MM/YYYY Format
Return to Discussion Resource Index
Post New Resource
Category: Regular Expressions


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use