Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Regular Expressions and VBScript

How It Works

The ReverseName2 function differs from the ReverseName function that you saw in an earlier example in this chapter in one key respect: It uses nongrouping parentheses to contain the whitespace characters entered between the first name and last name:

myRegExp.Pattern = “(\S+)(?:\s+)(\S+)”

This means that the first name is in $1 (as before), and the last name is in $2 (it was previously in $3). So when using the Replace() method, you must make appropriate adjustments to ensure that the last name (in $2) is displayed first, then the comma and space character, and then the first name (in $1):

Match = myRegexp.Replace(TestString, “$2, $1”)

Exercises

1.Modify the file TestForA.html so that it tests for a date value entered in the format MM/DD/YYYY.

2.Modify the file ReverseName.html so that it will accept only a sequence of alphabetic characters followed by one or more whitespace characters, followed by a sequence of alphabetic characters in the input box. If there is any additional input in the text box, an error message should be displayed requesting input in the specified format. Hint: As well as needing to modify the value of the Pattern property, you will need to amend the message displayed if there is not a match.

483

21

Visual Basic .NET and

Regular Expressions

Microsoft Visual Basic .NET provides powerful and flexible regular expression functionality. There are both significant similarities and significant differences between the regular expression support in Visual Basic .NET and that described earlier in VBScript. Visual Basic .NET regular expression support is more powerful and flexible.

The foundation on which Visual Basic .NET regular expression programming rests is the System.Text.RegularExpressions namespace, which is part of the .NET Framework Class Library.

In this chapter, you will learn the following:

How to use the classes and objects contained in the System.Text.RegularExpressions namespace

The meaning of the metacharacters supported in Visual Basic .NET

Examples shown in this chapter have been tested with Visual Studio 2003 and the

.NET Framework 1.1. I will assume that you have access to a copy of Visual Studio 2003 and have a working knowledge of at least the basics of Visual Basic .NET. It isn’t the intent of this chapter to provide a tutorial on the basics of using Visual Studio .NET 2003. However, if you do not have access to a copy of Visual Studio 2003, there are copies of the .exe files, which you can run, although you won’t be able to view and edit the Visual Basic .NET code.