Regular Expressions by .Net
String manipulation has always been a major part of a developers job - handling sourced (stored and posted) data, stripping and formatting it to suit our needs.
I've always found it tedious and time consuming to string it by using the common string variable methods.
ToUpper()
ToLower()
Replace()
SubString()
Remove()
Well, the list goes on - using SubString() and IndexOf() in conjunction with eachother was an early way of removing unwanted characters.
It could quickly becoming very complicated and notoriously faulty code:
i mean, stripping numerical characters out of an textbox's value for a Firstname can take a hell of a lot of time..Regular Expressions can help cut this down by a LARGE amount of time.
First off, i'm definitely not professing to be the guru on Regular Expressions - not by a long stretch - so please, if you find flaws or have ideas for enhancements feel free to comment.
public string CleanString(string input)
{
// Replace invalid characters with empty strings.
return Regex.Replace(input, @"[^\w\.@-]", "");
}
Simple? well, getting it right can at times be touch and go..
so now it's stripped? what about telling the user that they can only use alphabetical characters? again..Regular Expressions to the rescue.
public bool ValidateString(string input)
{
// Verifies input string only contain
// alphabetical charachters, upper/lower case
return Regex.IsMatch(input, @"^[a-zA-Z]$");
}
and my all-time favorite, email address validation..
public bool IsValidEmail(string input)
{
// Verifies input string contains a valid email address format
return Regex.IsMatch(input, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$);
}
There's tons of uses - if you, like me, aren't the best of the gurus with Regular Expressions there's tons of help on the interweb..my favorite is www.regexlib.net - can't go wrong on this, just bear in mind, these are user submitted examples, not all of them works anywhere close to what is expected.
As an example on some heavy(ish) string manipulation (don't mind this, it's only an example)
///
<summary>
/// validates a string in accordance with minimum and maximum length
/// - allows for whitespace but does required a minimum of 1 numeric character.
/// </summary>
/// <param name="pString"></param>
/// <param name="pMin"></param>
/// <param name="pMax"></param>
/// <returns>true/false</returns>
public bool IsValidPhoneNumber(string pString, int pMin, int pMax)
{
bool blnResult = false;
bool blnIsNumber = false;
bool blnWhitespace = false;
bool blnIsLetter = false;
// only attempt string validation if pString exists
if (pString != null)
{
try
{
if (pMin == 0 && pString.Length == pMin)
{
blnResult = true;
}
else if (pString.Length >= pMin && pString.Length <= pMax)
{
foreach (char chr in pString.ToString())
{
if (char.IsNumber(chr)) { blnIsNumber = true; }
if (char.IsWhiteSpace(chr)) { blnWhitespace = true; }
if (char.IsLetter(chr)) { blnIsLetter = true; }
}
if (!blnIsLetter)
{
if (blnWhitespace)
{
if (blnIsNumber)
{
blnResult = true;
}
}
else if (blnIsNumber)
{
blnResult = true;
}
}
}
}
catch (Exception eValidate)
{
throw eValidate;
}
}
return blnResult;
}
To me, that doesn't get much worse - could a Regular Expression here help?