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?

Published Wednesday, May 24, 2006 12:12 AM by Brian Madsen
Filed under: ,

Comments

# re: Regular Expressions by .Net

Hey Brian,

Love your work. I am also falling in love with regular expressions. (Correction, I have always loved, but not completely understood them, the more I use, the more I continue to use -- and love them).

Here is an example I came across:


private void splitEmailInfo(string completeEmail, out string emailAddress, out string senderName)
{
string pattern = "([a-zA-Z0-9_\\-])+(\\.([a-zA-Z0-9_\\-])+)*@((\\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\\]))|((([a-zA-Z0-9])+(([\\-])+([a-zA-Z0-9])+)*\\.)+([a-zA-Z])+(([\\-])+([a-zA-Z0-9])+)*))$";
Regex r = new Regex(pattern);
emailAddress = "";
senderName = "";

// Remove the <'s and >'s so we can extract the email address
completeEmail = completeEmail.Replace("<", "");
completeEmail = completeEmail.Replace(">", "");
if (r.Match(completeEmail).Success)
{
emailAddress = r.Match(completeEmail).Value;
completeEmail = completeEmail.Replace(emailAddress, "");
}
senderName = completeEmail.Trim();
}


You give this an email field 'Mr CRM<mrcrm@domain.com>' and it will return the two fields email and email name.

Tuesday, May 23, 2006 8:54 PM by Alan Toogood

# re: Regular Expressions by .Net

Hey Alan,

thanks for stopping by and sharing..

that's a handy Regex for sure, especially when dealing with office applications where the "sender" field sometimes contain those two - granted you can just that to reply easily but if you needed to seperate the two values for database or third party application use, references (to find username/useraccount maybe?).

great work...lets see what else comes out of the closet!

Tuesday, May 30, 2006 11:45 PM by Brian Madsen

# re: Regular Expressions by .Net

plz send the code for validating the url using textbox and c#.Net

Tuesday, February 06, 2007 10:57 PM by micheal

# re: Regular Expressions by .Net

Micheal - i'll write it up and post it over the weekend.

I don't have your email address (and don't show it here, spam bots will grab it and in no time you'll be wishing you hadn't) so i can't send it to you...

sorry for the delay in responding - didn't notice the request till today as it's a very old post of mine.

Friday, February 09, 2007 3:07 AM by Brian Madsen