Sample Code - Regular Expression
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);
}
}
}
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);
}
}
}
Labels: Regular Expression

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home