C # Regular Expression example

Source: Internet
Author: User

The following example shows the use of Regular Expresssions in C #. This program has basic validation scripts for validation easily useable in all programs.


/*
<HowToCompile>
Csc/r: System. Text. RegularExpressions. dll, System. dll Validation. cs
</HowToComplie>
*/


Using System. Text. RegularExpressions;
Using System;


Class Validation
{
Public static void Main ()
{
String strToTest;
Validation objValidate = new Validation ();

Console. Write ("Enter a String to Test for Alphabets :");
StrToTest = Console. ReadLine ();
If (objValidate. IsAlpha (strToTest ))
{
Console. WriteLine ("{0} is Valid Alpha String", strToTest );
}
Else
{
Console. WriteLine ("{0} is not a Valid Alpha String", strToTest );
}
}

// Function to test for Positive Integers.

Public bool IsNaturalNumber (String strNumber)
{
Regex objNotNaturalPattern = new Regex ("[^ 0-9]");
Regex objNaturalPattern = new Regex ("0 * [1-9] [0-9] *");

Return! ObjNotNaturalPattern. IsMatch (strNumber )&&
ObjNaturalPattern. IsMatch (strNumber );
}

// Function to test for Positive Integers with zero cumulative sive

Public bool IsWholeNumber (String strNumber)
{
Regex objNotWholePattern = new Regex ("[^ 0-9]");

Return! ObjNotWholePattern. IsMatch (strNumber );
}

// Function to Test for Integers both Positive & Negative

Public bool IsInteger (String strNumber)
{
Regex objNotIntPattern = new Regex ("[^ 0-]");
Regex objIntPattern = new Regex ("^-[0-9] + $ | ^ [0-9] + $ ");

Return! ObjNotIntPattern. IsMatch (strNumber )&&
ObjIntPattern. IsMatch (strNumber );
}

// Function to Test for Positive Number both Integer & Real

Public bool IsPositiveNumber (String strNumber)
{
Regex objNotPositivePattern = new Regex ("[^ 0-9.]");
Regex objPositivePattern = new Regex ("^ [.] [0-9] + $ | [0-9] * [.] * [0-9] + $ ");
Regex objTwoDotPattern = new Regex ("[0-9] * [.] [0-9] * [.] [0-9] *");

Return! ObjNotPositivePattern. IsMatch (strNumber )&&
ObjPositivePattern. IsMatch (strNumber )&&
! ObjTwoDotPattern. IsMatch (strNumber );
}

// Function to test whether the string is valid number or not
Public bool IsNumber (String strNumber)
{
Regex objNotNumberPattern = new Regex ("[^ 0-9.-]");
Regex objTwoDotPattern = new Regex ("[0-9] * [.] [0-9] * [.] [0-9] *");
Regex objTwoMinusPattern = new Regex ("[0-9] * [-] [0-9] * [-] [0-9] *");
String strValidRealPattern = "^ ([-] | [.] | [-.] | [0-9]) [0-9] * [.] * [0-9] + $ ";
String strValidIntegerPattern = "^ ([-] | [0-9]) [0-9] * $ ";
Regex objNumberPattern = new Regex ("(" + strValidRealPattern + ") | (" + strValidIntegerPattern + ")");

Return! ObjNotNumberPattern. IsMatch (strNumber )&&
! ObjTwoDotPattern. IsMatch (strNumber )&&
! ObjTwoMinusPattern. IsMatch (strNumber )&&
ObjNumberPattern. IsMatch (strNumber );
}

// Function To test for Alphabets.

Public bool IsAlpha (String strToCheck)
{
Regex objAlphaPattern = new Regex ("[^ a-zA-Z]");

Return! ObjAlphaPattern. IsMatch (strToCheck );
}

// Function to Check for AlphaNumeric.

Public bool IsAlphaNumeric (String strToCheck)
{
Regex objAlphaNumericPattern = new Regex ("[^ a-zA-Z0-9]");

Return! ObjAlphaNumericPattern. IsMatch (strToCheck );
}


}


There is another simple way to perform these validation think of it while the next article comes.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.