Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Leverage Regular Expressions
Use .NET regular expressions to simplify and optimize your text-processing applications.
by Francesco Balena

January 2003 Issue

Technology Toolbox: VB.NET, C#, VB6

ADVERTISEMENT

Regular expressions provide a simple and efficient way—far beyond the capabilities most conventional programming languages offer—to search and replace text. However, relatively few developers are familiar with this powerful tool. I'll explain how you can leverage regular expressions from VB.NET to solve common parsing problems, such as extracting words, numbers, and dates out of a text document. Most of what you'll learn applies to previous Visual Basic versions too (see the sidebar, "Use Regular Expressions From VB6").

A regular expression is a search pattern that identifies what you're looking for in a source string. You're familiar already with the simplest forms of search patterns, such as the one you can use at the command prompt when you copy or delete files (for example, DEL A??.*). You can also use regular-expression–like patterns with SQL's LIKE operator in WHERE clauses, or VB's LIKE operator:

If name Like "[AEIOU]???" Then
   ' name is a 4-char word that
   ' starts with uppercase vowel
End If

Any .NET regular-expression operation requires that you pass the search pattern to the Regex object's constructor:

Dim re As New Regex("[AEIOU]\w\w\w")

All the regular-expression classes are in the System.Text.RegularExpressions namespace, so all code snippets in this article assume you've used a proper Imports statement. The previous regular expression searches for a string that starts with an uppercase vowel and is followed by three alphanumerical characters (the \w sequence). This first example gives you an idea of why most developers consider regular expressions cryptic and why their use tends to be confined to specialized text-processing languages such as Perl and Awk. The truth is, you can become familiar with regular expressions in a matter of a few hours.

Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home