ObjectSharp Blogs

You are currently viewing

Dave Lloyd's 2 Cents

A .NET Developer's Perspective


Regular Expressions

Regular expressions are a very powerful tool for validating or parsing a string. I don't claim to be an expert in the use of Regular expressions by any means. But I have used them with great success on a number of occasions. They are very useful. I have used them to validate a special URL that is used to create hyperlinks to forms within a smart client application, or to parse the syntax of a Powerbuilder DataWindow to name just two.

In ASP.NET there is a Regular Expression validator control. This control lets you validate input based on Regular Expressions. The nice thing about this control is the hard work is done for you for a variety of different strings. For example there is a sample expression for internet email addresses. \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* and one for an Internet URL http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

If you want to use Regex in .NET (C# or VB.NET) getting the expression right is the tough part, as you can see from the expressions above. The rest is pretty easy. To help you out there is a tool written by Eric Gunnerson called Regular Expression Workbench that I have found very useful on many occasions. You can down load it from gotdotnet. This wonderful tool will help you format your expression, interpret or execute your expression on a sample string, even tell you how efficient the operation is.

For those of you who don't really know how to use Regular Expressions here is a simple example to get you started. Using this example you can test a string for swear words.

First: to use Regular Expressions you must use the System.Text.RegularExpressions namespace.

Then create a regular expression and call the match method to search your string.

 using System.Text.RegularExpressions;
 Regex r;                                                     //Declare Regular expression
 r = new Regex("hell");                                //Create expression
 Match m = r.Match(this.textBox1.Text);    //Call match method

 if (m.Success)                                           //Check to see if a match was made
   {
      MessageBox.Show("You can't say that.");
   }
 else
   {
      MessageBox.Show("nothing found.");
   }

The above example works to find the word hell but it will also find it inside the word hello. You can add the escape character /b to denote word boundaries. For example if your expression is /bhell/b it will only match on the word hell. /b has two uses in a regular expression it's a word boundary. When used within a [ ] character class it refers to the backspace character.

Fine so you can now validate a string to keep users from entering a swear word. "But Dave there are many words I need to check for.", you say. Not to worry this can be done in the same expression using a pipe. If you separate each word you want to find with a pipe Match( ) will look for all the words listed. See the example below.

using System.Text.RegularExpressions;
Regex r;                                                    //Declare Regular expression
   
 r = new Regex("
\\bhell\\b|\\bfrig\\b");        //Create expression
 Match m = r.Match(this.textBox1.Text);  //Call match

 if (m.Success)                                         //Check to see if a match was made
 {
  MessageBox.Show("You can't say that.");
 }
 else
 {
  MessageBox.Show("nothing found.");
 }

Comments

  • dave May 17, 2004 11:48 PM

    Roy Osherove, according to many in the "blogosphere"(I hate that word), wrote THE Regular Expression evaluator tool. It's got tons of features. I have it on my "to-do" list:

    http://sourceforge.net/projects/regulator/

  • dave May 18, 2004 12:27 AM

    I have not used that utility. I found the Regular Expression Workbench a long time ago and it did the job for me, so I have never gone looking for any others.

    Thanks for pointing it out Jason. I'll have to check it out.

  • dave June 29, 2004 7:32 PM

    Dave,

    I think the regulator is like my utility on steroids. Roy integrated my regex analysis code in a while back, and I think it's very useful.

  • dave August 4, 2004 10:24 AM

    The expression you've given for URL validation does not make sure it is a valid URL, I have, in fact, not ever been able to find one that does!! Your expression will validate http://www.goo or http://b.bb as valid urls!!

  • dave August 10, 2004 12:42 PM

    Ian...

    One of these may do what you want it to: http://regexlib.com/Search.aspx?k=url

  • dave February 26, 2005 10:11 AM

    Ian...

    http://www.goo IS a valid url. just beacuse the TLD of goo isnt in use dosnt mean that it isnt following RFC 1738 format for syntax.

    The expression offerd here is to validate that the URL fits syntax, not that the url exists, or can possibly exist.

    for example, it also validates http://alex-is-super-cool.you-should-all-respect-him.com but unfortionatly that dosnt exist (nor is the statement true in any way....)

  • dave October 9, 2005 7:06 AM

    jgjgj

  • dave October 9, 2005 7:06 AM

    jgjgj

  • dave December 27, 2005 8:25 AM

    hfgjbhmj

  • dave July 8, 2006 6:15 AM

    sdf

  • dave September 1, 2006 6:38 AM

    Cheers for pasting this dude! Works great

  • dave November 22, 2006 6:28 AM

    i found it useful

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS