Joben.Rara.blogs(".NET")

There are two ways to write error-free programs; only the third one works.

Recycling: Short Introduction to Regular Expression

I recently wrote a short article as an overview to why regular expressions should be in every developer's toolbox. I actually left the real discussion of regex to other experts so I used a lot of link-love. [:-)]

Seeing that Mike Malone posted an advanced regex article, I decided to put up that short intro here.

What are regular expressions and why should I (a.k.a. developer) care?

If you’re looking for a definition, try this http://en.wikipedia.org/wiki/Regular_expression .

However I think it’s better to show you rather than tell you what a regular expression is.

What if you wanted to search your code for all instances of btn_Ok, btn_Cancel, btn_Reset and change them into OkButton, CancelButton, ResetButton. Looking at the task, you’ll see that it’s a fairly simple pattern and you can do this with three runs of Find-and-Replace. But what if you had to change 10 of these? 100? Suppose there was a way to do this with just one run, would you like to know how?

The answer would be “Use a Regular Expression (during a Find and Replace)”.

Assuming that our favorite code editor was capable of using a standard regular expression, what would the “pattern matching language” look like.

(btn_)(\w*)

this will match all strings that have the pattern btn_Ok, btn_Cancel, etc. It uses groupings so it could use it during replace.

\2Button

this replaces the strings matching the pattern with the text after “btn_” and the word “Button”

Using vim, this would be :%s:\(btn_\)\(\w*\):\2Button:g

In Visual Studio, the syntax is a bit different. I’ll mention why later on.

{btn_}{:a*}

\2Button

Find-And-Replace Dialog

Regular expression only requires you know a few rules to get you started. Check out The absolute bare minimum every programmer should know about regular expressions as a starter.

Pattern matching is not only useful for find and replace, you can also use it to validate user input. Validate User Input with Regular Expressions (VB.NET)

The following links have sample regular expressions found in input validation scenarios like a valid email, a valid Telephone number, a valid URL, etc.

5 Regular Expressions Every Web Programmer Should Know, Regular Expressions that are good to know

Learn about regular expressions in .NET

MSDN Webcast: Using Regular Expressions (Part 2 of 2): Metacharacters (Level 200)

Regular Expression Language Elements in the .NET framework

.NET Developer's Guide to Regular Expression Examples

MSDN forum on Regular Expressions

Use regular expressions in Microsoft Word

Add power to Word searches with regular expressions

Putting regular expressions to work in Word

Test your regular expressions with these online tools.

Regular Expression Tester (dotnetcoders.com)

Regex Tester (regexlib.com)

Or you can download Expresso, a tool “useful for learning how to use regular expressions and for developing and debugging regular expressions prior to incorporating them into C# or Visual Basic code.” It can even generate the proper C#, VB, and Managed C++ code for the regular expression you’re testing.

And Visual Studio does not play nice

The Visual Studio IDE and Regular Expressions details how Visual Studio supports regular expressions in its find-and-replace but uses a different “dialect” of regular expressions. (Bad VS, bad!). Michael Flanakin posts a collection of Regular Expressions for Visual Studio Code Analysis that uses Visual Studio’s particular syntax. Here’s a beginner’s guide to using  VS Find and Replace with regular expressions.

Posted: 06-25-2007 4:22 PM by velocity | with 1 comment(s)
Filed under: ,

Comments

willydavidjr said:

What I like with regex is that you can use it's Regex.Replace just like the String.Replace. =)

# June 27, 2007 8:20 PM