Everything is achievable through technology

C# Regx Validations

Get EmailID from URL Regx

string emailPattern = @"^?([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$";
Match match = Regex.Match(theRealURL.ToString(), emailPattern);
if (match.Success)
campaignEmail = match.Value;
// Source Code starts
using System.Text.RegularExpressions;
using System;

csc /r:System.Text.RegularExpressions.dll,System.dll Validation.cs

class Validation
public static void Main()
String strToTest;
Validation objValidate=new Validation();
Console.Write("Enter a String to Test for Alphabets:");
Console.WriteLine("{0} is Valid Alpha String",strToTest);
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) &&
// Function to test for Positive Integers with zero inclusive
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-9-]");
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) &&
// 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) &&
// 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);
// Function To test for Email id.
public bool IsEmail(String emailToCheck)
Regex objAlphaNumericPattern=new Regex("^((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})$");
return !objAlphaNumericPattern.IsMatch(emailToCheck);
public string removenewline(string newlinestring)
string replacementString="";
string result = Regex.Replace(newlinestring, @"\r\n?|\n", replacementString);
//result = newlinestring.Replace(System.Environment.NewLine, replacementString);
return result;
string str = "{name} is {age} years old";
Dictionary dict = new Dictionary();
dict.Add("{name}", "nikhil");
dict.Add("{age}", "24");
var newstr = dict.Aggregate(str, (current, value) => current.Replace(value.Key, value.Value));

// Source Code End

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s