Getting Started With Regular Expressions In this article, Jim will introduce you to regular expressions, what they are, why you would want to use them, and finally, how you can begin putting them to work in Visual Studio .NET. Regular expressions. The name doesn't conjure up any grandiose ideas about what they are all about. How could it with the word "regular" in the title? For those of you who struggled to learn how to use them, you're probably thinking they should be renamed irregular expressions. How could something that looks like this... ^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$
...be called regular??? (That pattern, by the way, will validate nearly 99% of all e-mail addresses, but more about that later.) I've broken this article down into three sections. Describe regular expressions Analyze some common patterns Implement common expressions in Visual Studio .NET. At the end of the article I've listed resources including Web sites, books, and regular expression software. What Are Regular Expressions? A regular expression allows you to efficiently search for strings within other strings using a pattern matching expression. You've probably used a regular expression before and didn't even know it. For example, have you ever used *.txt to search for or filter a list of files? If so, congratulations! You're a regular expression user! Regular expressions play a key role in all kinds of text-manipulation tasks. Common uses include searching (matching) and search-and-replace. You can also use common expressions to test for specific conditions in a string, text file, Web page, or XML stream. You could use regular expressions as the basis for a program that filters spam from incoming mail. In this type of situation, you might use a regular expression to determine whether the "From:" e-mail address is the e-mail address of a known spammer. As a matter of fact, many e-mail filtering programs use regular expressions for exactly this reason. One of the drawbacks to using a regex is that they tend to be easier to write than they are to read. Here's an example of a very common regex pattern. ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
If this pattern looks overwhelming right now, don't worry, you'll know what it means by the time you finish reading this article. You construct regex patterns using a series of different characters called metacharacters. Table 1 lists the most common metacharacters. | & | | 
By: Jim Duffy Jim Duffy is founder and president of TakeNote Technologies, an award-winning training, consulting, and software development company specializing in .NET software developer training and helping clients create business solutions with Microsoft enterprise technologies like Virtual Earth. Jim's expertise is with Visual Studio, Visual Basic, ASP.NET, SQL Server and Visual FoxPro-to-.NET conversions. He has a BS degree in Computer and Information Systems and over 25 years of programming and training experience. He is an energetic trainer, skilled developer, and has been published in leading developer-oriented publications.
Jim is a Microsoft Regional Director, a Microsoft MVP award recipient since 2003, an INETA speaker, and is an entertaining and popular speaker at regional user groups and international developer conferences. He is also a co-host of Computers 2K9, a call-in radio show on WRBZ (AM 850), 850 The Buzz, in Raleigh, NC.
You can find additional information about Jim, TakeNote Technologies, links to his blog, as well as a public training class schedule, on-site training information, consulting information, and software development services at www.takenote.com.
jduffy@takenote.com |