Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

August 23, 2000 12:00 AM

Understanding VBScript: Real-World Uses of Regular Expressions

Windows IT Pro
InstantDoc ID #9800
Rating: (0)
Downloads
9800.zip

VBScript—Novice

Last month, I introduced you to VBScript's regular expressions. This month, I present handy code that you can use to enhance your real-world VBScript applications. Specifically, I show you how to use regular expressions to rewrite two commonly used VBScript functions: InputBox and Replace. You can use the rewritten InputBox function to create dialog boxes that accept only data that matches a regular expression you specify. You can use the rewritten Replace function to perform powerful search-and-replace operations.

Rewriting the InputBox Function
In the Web-exclusive sidebar "Building a Better InputBox Function" in my October 1999 column, I created an improved version of the InputBox function called InputBoxEx. This user-defined function not only produced a dialog box but also validated the data users entered in it. You can use parts of the InputBoxEx code to create a function called InputBoxRegEx. This new user-defined function produces a dialog box, then determines whether the entered text matches the specified regular expression pattern.

Listing 1, page 2, contains the InputBoxRegEx code. The code begins with the definition of the InputBoxRegEx function. InputBoxRegEx takes the same arguments as InputBox (i.e., prompt, title, and default) plus a new one (i.e., re). The prompt and title arguments specify the dialog box's message and title, respectively. The default argument specifies the default response that appears in the edit box (i.e., the box in which users enter their input). The re argument specifies the pattern you want to test the entered data against.

After the function initializes the InputBoxRegEx variable by setting it to an empty string, the function calls InputBox. InputBox displays a dialog box for users and returns the text that they enter in the str variable. If users cancel the dialog box, InputBox returns an empty string.

If the string isn't empty, InputBoxRegEx uses VBScript's CreateObject function to create an instance of the RegEx object. The function uses RegExp's Pattern property to set the regular expression pattern (i.e., the pattern in the re argument), then sets the IgnoreCase property to True for a case-insensitive search.

Finally, InputBoxRegEx applies RegExp's Test method to determine whether the text in the str variable matches the regular expression pattern. If a match occurs, the function sets the text to the InputBoxRegEx variable. If no match occurs, the function returns an empty string.

How might you use InputBoxRegEx in your job? Suppose you must ask users to enter an email address. With the code in Listing 2, page 2, you can make sure users enter a valid email address. If the email address matches the pattern you specify, a message box displays the entered email address. If the email address doesn't match the pattern, the message Not a valid email address appears.

Modifying InputBoxRegEx
You can easily modify InputBoxRegEx to meet your needs. For example, in its current form, InputBoxRegEx returns only one piece of information: either the dialog box's content (if a match occurs) or an empty string (if a match doesn't occur). You can modify this function so that it also returns a Boolean value specifying the result of the match. To make this modification, InputBoxRegEx needs to return an array of values. You can use many approaches to incorporate this array, including the approach that the InputBoxRegExArray code in Listing 3 illustrates.

In this approach, you adapt the InputBoxRegEx function in Listing 1 in four areas:

  1. Add the RESULT_TEXT and RESULT_TEST constants that callout A in Listing 3 shows. These user-defined constants provide an elegant way to identify the exact item in the array that contains the specific chunk of information you need. By assigning the RESULT_TEXT constant the value of 0, it represents the first item in the array when you use the code a(RESULT_TEXT). Similarly, by assigning the RESULT_TEST constant the value of 1, it represents the second item in the array when you use the code a(RESULT_TEST).

  2. Replace the line
    InputBoxRegEx = ""
    with the code at callout B in Listing 3. The Array function creates the array.

  3. Replace the lines
    If regexp.Test(str) Then
    InputBoxRegEx = str
    with the code at callout C in Listing 3. This code fills the array. The first item in the array is the content of the dialog box; the second element is the Boolean value.

  4. Add the code at callout D in Listing 3. This code returns the array and sets it to the InputBoxRegEx variable.

    After you make these modifications, you can use InputBoxRegExArray with code like that in Listing 4. This code prompts InputBoxRegExArray to execute, then displays its results.

    This example is only one of many ways you can modify InputBoxRegEx. However, if you're a novice scriptwriter, I recommend that you maintain the role and position of InputBoxRegEx's prompt, title, and default arguments because of their correlation with their counterparts in InputBox.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.