The RegularExpressions Class

Team Fly 

Page 547

If the letters (or numbers) are consecutive, you can use the range operator and specify the first and last letter in the range. The following expression matches all uppercase characters:

 [A-Z] 

while the following matches all numeric digits:

 [0-9] 

The expression [1357] means any of the digits ''1", "3" or "5" or "7."

ISBNs are made up of 9 numeric digits followed by a check digit, which can be either a numeric digit or the character X. The following expression locates ISBN values in the text (but it doesn't validate their check digit, of course):

 [0-9]{9}[0-9X] 

This pattern instructs the regular expression engine to locate 9 numeric digits followed by another numeric digit, or the character X. The expression [0-9]{9} means 9 digits. The next character can be either a digit or the letter X.

Notice the content of the second pair of square brackets: it matches a character in the range 0–9, or the character "X." Digits are so common that there's a special metacharacter for them. This is the \d metacharacter. The following regular expression will also locate ISBNs in a text:

 \d{9}[\dX] 

This expression will match all runs of 10 digits in the text, even if they're part of a very large, unformatted number. If the text contains the number 390102188541, then the previous regular expression will report the first 10 digits as a match. To avoid erroneous matches, we must also use the \b metacharacter, which specifies the beginning or the end of a word. Our final regular expression for locating ISBN values is:

 \b\d{9}[\dX]\b 

There's quite a bit about regular expressions and we'll return to the topic of building regular expressions shortly, but first we'll take a closer look at the RegularExpressions class. This class belongs to the System.Text namespace and it exposes all the functionality you'll need to use regular expressions in your .NET applications.

The RegularExpressions Class

Now that you have a general idea of what regular expressions are and how to locate general patterns of text, we can explore the basic functionality of the RegularExpressions class. To exploit the functionality of regular expressions in your code, you must import the System.Text.RegularExpressions class to your project:

 Imports System.Text.RegularExpressions 

and then create an object of the RegEx type:

 Dim RX As Regex 
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