Friday, May 1, 2009

Regular Expression in .Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace PubishApps
{
/************************************************
* Topic : Usage of RegularExpression in .Net
* Reference System.Text.RegularExpressions.
* Author : kalit sikka
* Summary: This regex helper class is design to provide set of static methods to validate commonly used patterns like Email-ID, URL, IP Address, Social Security number, Zip codes etc.
* You just need to add this .cs file in your project to use any of the method in it.
* For : Personal Blog
* **********************************************/
///
/// This helper class contain general regex static methods which in today to today coding tasks
///

public class RegexHelper
{
public static bool IsValidEmailID(string sEmailID)
{
Regex oEmail =
new Regex(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$");
return oEmail.IsMatch(sEmailID);
}
public static bool IsValidURL(string sUrl)
{
Regex oURL =
new Regex(@"^[a-zA-Z0-9\-\.]+\.(comorgnetmileduCOMORGNETMILEDU)$"); // you add more here like au, in
return oURL.IsMatch(sUrl);
}
public static bool IsValidPhoneNumber(string sPhone)
{
Regex oPhone =
new Regex(@"^[2-9]\d{2}-\d{3}-\d{4}$"); // US Phone - like 800-555-5555 333-444-5555 212-666-1234
return oPhone.IsMatch(sPhone);
}
public static bool IsValidIndianMobile(string sMobile)
{
Regex oMobile =
new Regex(@"^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}98(\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$"); // Indian Mobile - like +919847444225 +91-98-44111112 98 44111116
return oMobile.IsMatch(sMobile);
}

public static bool IsValidUKMobile(string sMobile)
{
Regex oMobile =
new Regex(@"^07([\d]{3})[(\D\s)]?[\d]{3}[(\D\s)]?[\d]{3}$"); // UK Mobile - like 07976444333 07956-514333 07988-321-213
return oMobile.IsMatch(sMobile);
}
public static bool IsValidUSZipCode(string sZipCode)
{
Regex oZipCode =
new Regex(@"^\d{5}$"); // ZipCode - like 33333 55555 23445
return oZipCode.IsMatch(sZipCode);
}
public static bool IsValidIPAddress(string sIPAddress)
{
Regex oIP =
new Regex(@"^(25[0-5]2[0-4][0-9][0-1]{1}[0-9]{2}[1-9]{1}[0-9]{1}[1-9])\.(25[0-5]2[0-4][0-9][0-1]{1}[0-9]{2}[1-9]{1}[0-9]{1}[1-9]0)\.(25[0-5]2[0-4][0-9][0-1]{1}[0-9]{2}[1-9]{1}[0-9]{1}[1-9]0)\.(25[0-5]2[0-4][0-9][0-1]{1}[0-9]{2}[1-9]{1}[0-9]{1}[0-9])$"); // IP Address - like 127.0.0.1 255.255.255.0 192.168.0.1
return oIP.IsMatch(sIPAddress);
}

public static bool IsValidTime(string sTime)
{
Regex oTime =
new Regex(@"^(20212223[01]dd)(([:][0-5]d){1,2})$");
return oTime.IsMatch(sTime);
}
public static bool Is24HourTimeFormat(string sTime)
{
Regex oTime =
new Regex(@"^(([0-1]?[0-9])([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$"); // like - 12:15 10:26:59 22:01:15 - Seconds are optional here
return oTime.IsMatch(sTime);
}
public static bool Is12HourTimeFormat(string sTime)
{
Regex oTime =
new Regex(@" ^ *(1[0-2][1-9]):[0-5][0-9] *(apAP)(mM) *$"); // like - 12:00am 1:00 PM 12:59 pm
return oTime.IsMatch(sTime);
}
public static bool DataFormat(string sDate)
{
// dd/MM/yyyy format with leap year validations
Regex oDate =
new Regex(@"^(((0[1-9][12]\d3[01])\/(0[13578]1[02])\/((1[6-9][2-9]\d)\d{2}))((0[1-9][12]\d30)\/(0[13456789]1[012])\/((1[6-9][2-9]\d)\d{2}))((0[1-9]1\d2[0-8])\/02\/((1[6-9][2-9]\d)\d{2}))(29\/02\/((1[6-9][2-9]\d)(0[48][2468][048][13579][26])((16[2468][048][3579][26])00))))$");
return oDate.IsMatch(sDate);
}
public static bool IsNumeric(string sNum)
{
// Natural Number
Regex oNum =
new Regex(@"0*[1-9][0-9]*");
return oNum.IsMatch(sNum);
}
public static bool IsAlpha(string sValue)
{
// Alpha value
Regex oValue =
new Regex(@"[^a-zA-Z]");
return oValue.IsMatch(sValue);
}
public static bool IsAlphaNumeric(string strToCheck)
{
Regex oCheck=new Regex("[^a-zA-Z0-9]");
return oCheck.IsMatch(strToCheck);
}

public static bool IsStrongPassword(string sPassword)
{
// Check Password with atleast 8 characters, no more than 15 characters, and must include atleast one upper case letter, one lower case letter, and one numeric digit.
Regex oPassword =
new Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}$");
return oPassword.IsMatch(sPassword);
}
public static bool IsSocialSecurityNumber(string sValue)
{
// U.S. social security numbers, within the range of numbers that have been currently allocated
Regex oValue =
new Regex(@"^(?!000)([0-6]\d{2}7([0-6]\d7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}");
return oValue.IsMatch(sValue);
}
public static bool IsVISACreditCard(string sValue)
{
// Validate against a visa card number
Regex oValue =
new Regex(@"^([4]{1})([0-9]{12,15})$");
return oValue.IsMatch(sValue);
}
public static bool IsISBNumber(string sValue)
{
// ISBN validation expression
Regex oValue =
new Regex(@"^\d{9}[\dX]$");
return oValue.IsMatch(sValue);
}
public static bool IsDollarAmount(string sAmt)
{
// Dollar decimal amount with or without dollar sign
Regex oAmt =
new Regex(@"^\$?\d+(\.(\d{2}))?$");
return oAmt.IsMatch(sAmt);
}
///
/// Method to return string array of splitted values from string
///

/// string value [KabirSachinJohnMarkDavidkevinNash]
/// separator in the string value
///
public static string[] mSplitString(string value, string separator)
{
string[] values = new string[25];
if(!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(separator))
{
values = System.Text.RegularExpressions.Regex.Split(value, separator);
return values;
}
return null;
}
///
/// To Remove specfic string from Collection of string
///

/// KabirSachinJohnMarkDavidkevinNash
/// separator in the string value
///
public static string mRemoveStringFromCollection(string strNames, string sep, string specificStr)
{
List list = new List();
string strValue = String.Empty;
if (!string.IsNullOrEmpty(strNames))
{
list.AddRange(Regex.Split(strNames, sep));
foreach (string str in list)
{
if (!str.Contains(specificStr))
{
if (string.IsNullOrEmpty(strValue))
{
strValue = str;
}
else
{
strValue = strValue + sep + str;
}
}
}
return strValue;
}
return strNames;
}

}
}

1 comment:

Locations of visitors to this page