Writing Regular Expressions

Team Fly 

Page 544

Writing Regular Expressions

Before examining the methods of the RegularExpressions class and how to program it, we'll review the process of building regular expressions. To experiment with regular expressions as you read through the material of this tutorial, use the RegExEditor application, which is described later in this chapter. This application, shown in Figure 19.1, is a simple text editor that supports the usual editing operations (copy/cut/paste); its Find command allows you to search with regular expressions as well as literals. The regular expression in the Search For box of the Find & Replace dialog box locates words that begin with "t," followed by three characters (any three characters) and ending with the character "e."The \w construct in the regular expression is a so-called metacharacter that matches a word character (everything except spaces and punctuation symbols). This metacharacter is followed by a count in curly brackets. The subexpression \w{3} stands for three consecutive word characters. The \b construct is another metacharacter that matches word boundaries. By placing the \b metacharacter at the beginning and the end of the regular expression, we specify that the matches should be complete words. If you're totally unfamiliar with regular expressions you may find them quite odd, but don't give up yet. We'll explore all metacharacters in the following sections, starting with the simpler ones and progressing to more complicated ones.

You can also use the RegularExpressions project to experiment with regular expressions. This project demonstrates a few of the more advanced topics, such as using groups in the regular expressions and performing replace operations using regular expressions. You can also use it with simple regular expressions such as the ones we'll build in the following sections. Enter the regular expression to match against the text in the upper TextBox control (Search Pattern box) and the text to be searched in the large TextBox control (Text box), then click the Find First Match button to locate the first match. After that, keep clicking the Find Next Match button to locate the next match in the text. Each time a match is found, the matched text will be highlighted on the control with the text. For the time being, you can ignore the Replace Pattern box and the Replace Matches button, as well as the Groups box at the bottom of the form. We'll look at the function of these two buttons later in the chapter, when we'll discuss replacement operations with regular expressions. The Groups box contains the matches of complicated regular expressions that contain groups, which we'll cover later. Simple regular expressions, like the ones we're going to discuss in the introductory sections, contain a single group and a single capture.

image

FIGURE 19.1 Matching regular expressions with the RegExEditor project

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net