Regular Expressions In C#

C# Regex class provides functions to implement regular expressions. The code example include how to use Regular Expressions in C# for IsNaturalNumber, IsWholeNumber, IsPositiveNumber, IsInteger, IsNumber, IsAlpha, and IsAlphaNumeric.

C# Regex class provides pattern matching funcions in form of regular expressions. The source code examples in this article show how to use Regular Expressions to validate different inputs. The code provides methods to validate Alphabet, AlphaNumeric, Integer, Postive Integers, Floating point numbers and so on.
 
Explanation of Regular Expressions
 
Regular expressions are used to match specified input or validate a format in the source string.
 
Examples:
 
Pattern #1
  1. Regex objNotNaturalPattern=new Regex("[^0-9]");
Pattern #2  
  1. Regex objNaturalPattern=new Regex("0*[1-9][0-9]*");
Pattern #1 will match strings other than 0 to 9.
  • The ^ symbol is used to specify not condition.
  • the [] brackets if we are to give range values such as 0 - 9 or a-z or A-Z
In the above example, input 'abc' will return true and '123' will return false.
 
Pattern #2 will match for strings that are natural numbers. Natural numbers are the numbers that are always greater than 0. The pattern 0* tells that a natural number can be prefixed with any number of zero's or non zero's. The next [1-9] tells that it should contain atleast one number from 1 to 9 followed by any numbers of 0-9's.
 
In this case, input '0007' will return true and '00' will return false.
 
Basic operators to understand in Regex: 
 
  • "*" matches 0 or more patterns
  • "?" matches single character
  • "^" for ignoring matches.
  • "[]" for searching range patterns. 
Here is the complete source code example that provides functions to to check IsNaturalNumber, IsWholeNumber, IsPositiveNumber, IsInteger, IsNumber, IsAlpha, and IsAlphaNumeric.   
  1. // Source Code starts  
  2. using System.Text.RegularExpressions;  
  3. using System;  
  4. /* 
  5. <HowToCompile> 
  6. csc /r:System.Text.RegularExpressions.dll,System.dll Validation.cs  
  7. </HowToComplie> 
  8. */  
  9. class Validation  
  10. {  
  11.     public static void Main()  
  12.     {  
  13.         String strToTest;  
  14.         Validation objValidate = new Validation();  
  15.         Console.Write("Enter a String to Test for Alphabets:");  
  16.         strToTest = Console.ReadLine();  
  17.         if (objValidate.IsAlpha(strToTest))  
  18.         {  
  19.             Console.WriteLine("{0} is Valid Alpha String", strToTest);  
  20.         }  
  21.         else  
  22.         {  
  23.             Console.WriteLine("{0} is not a Valid Alpha String", strToTest);  
  24.         }  
  25.     }  
  26.     // Function to test for Positive Integers.   
  27.     public bool IsNaturalNumber(String strNumber)  
  28.     {  
  29.         Regex objNotNaturalPattern = new Regex("[^0-9]");  
  30.         Regex objNaturalPattern = new Regex("0*[1-9][0-9]*");  
  31.         return !objNotNaturalPattern.IsMatch(strNumber) &&  
  32.         objNaturalPattern.IsMatch(strNumber);  
  33.     }  
  34.     // Function to test for Positive Integers with zero inclusive   
  35.     public bool IsWholeNumber(String strNumber)  
  36.     {  
  37.         Regex objNotWholePattern = new Regex("[^0-9]");  
  38.         return !objNotWholePattern.IsMatch(strNumber);  
  39.     }  
  40.     // Function to Test for Integers both Positive & Negative   
  41.     public bool IsInteger(String strNumber)  
  42.     {  
  43.         Regex objNotIntPattern = new Regex("[^0-9-]");  
  44.         Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");  
  45.         return !objNotIntPattern.IsMatch(strNumber) && objIntPattern.IsMatch(strNumber);  
  46.     }  
  47.     // Function to Test for Positive Number both Integer & Real   
  48.     public bool IsPositiveNumber(String strNumber)  
  49.     {  
  50.         Regex objNotPositivePattern = new Regex("[^0-9.]");  
  51.         Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");  
  52.         Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");  
  53.         return !objNotPositivePattern.IsMatch(strNumber) &&  
  54.         objPositivePattern.IsMatch(strNumber) &&  
  55.         !objTwoDotPattern.IsMatch(strNumber);  
  56.     }  
  57.     // Function to test whether the string is valid number or not  
  58.     public bool IsNumber(String strNumber)  
  59.     {  
  60.         Regex objNotNumberPattern = new Regex("[^0-9.-]");  
  61.         Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");  
  62.         Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");  
  63.         String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";  
  64.         String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";  
  65.         Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");  
  66.         return !objNotNumberPattern.IsMatch(strNumber) &&  
  67.         !objTwoDotPattern.IsMatch(strNumber) &&  
  68.         !objTwoMinusPattern.IsMatch(strNumber) &&  
  69.         objNumberPattern.IsMatch(strNumber);  
  70.     }  
  71.     // Function To test for Alphabets.   
  72.     public bool IsAlpha(String strToCheck)  
  73.     {  
  74.         Regex objAlphaPattern = new Regex("[^a-zA-Z]");  
  75.         return !objAlphaPattern.IsMatch(strToCheck);  
  76.     }  
  77.     // Function to Check for AlphaNumeric.  
  78.     public bool IsAlphaNumeric(String strToCheck)  
  79.     {  
  80.         Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");  
  81.         return !objAlphaNumericPattern.IsMatch(strToCheck);  
  82.     }  
  83. }  
  84. // Source Code End