devxlogo

Create a better InStr function with VBScript regular expressions

Create a better InStr function with VBScript regular expressions

Regular expressions offer an extremely powerful way to search and replace text inside a string using sophisticated search criteria, far behind the capabilities of VB’s InStr and Replace functions. For example, you can search for whole words, or find a match with a range of characters (for example, a digit or any uppercase character), and can quickly locate and count all the occurrences of the searched string. Regular expressions are so important for many programming tasks that there are at least a couple of programming languages – Awk and Perl – whose main purpose is making regular expressions available to developers.

If you’ve installed VBScript version 5 or later – which is automatically installed with any version of Microsoft Internet Explorer starting at version 4.0 – you can take advantage of the powerful regular expression engine that comes with it. This engine is available to all VBScript developers – for example, from within Windows Script Host and Active Server Pages applications – and is even available to Visual Basic developer. All you have to do to use this regular expression engine from within your VB apps is add a reference to the Microsoft VBScript Regular Expressions type library.

The main object in the type library is the RegExp object, which represents a regular expression pattern. A pattern identifies the text you are searching for. You can use several special characters in the pattern to search for a category of characters (e.g. a symbol or a non-digit) or to specify where the searched string should appear in the text (e.g. at the beginning of a word, or just before the end of the line). To put regular expressions to good use you must learn how to use these special sequences of characters, a job that can be quite daunting and far beyond the scope of this simple tip, that is meant to work just an introduction to using the regular expression type library.

The main property of the RegExp object is Pattern, to which you assign the regular expression pattern soon after creating the object. You can also set the IgnoreCase property to True or False, depending on whether you want to perform case insensitive searches. Another important property is Global, that should be set to True if you want to find all the occurrences of the pattern, or False if you just want to test whether the pattern is contained at least once in the source string. Finally, the Execute method takes the source string – that is, the string inside which you’re searching the pattern – and returns a collection of Match objects. Each Match object represents an occurrence of the pattern, and exposes properties such as Value (the occurrence found), Length (number of characters in the occurrence), and FirstIndex (the position of the occurrence in the source string).

As an example of how to use the RegEx object, the following code searches all the occurrences of an uppercase character followed by three digits:

' NOTE: this code requires a reference to the '       Microsoft VBScript Regular Expression type library'Dim re As New RegExpDim ma As Matchre.Pattern = "[A-Z][0-9][0-9][0-9]"    ' uppercase char followed by 2 digitsre.IgnoreCase = False                  ' case sensitive searchre.Global = True                       ' find all the occurrencesFor Each ma In re.Execute(txtSource.Text)    Print "Found '" & ma.Value & "' at index " & ma.FirstIndexNext

Because the special d reqular expression sequence means “a digit character”, the Pattern property in the above example could be assigned also as follows:

re.Pattern = "[A-Z]ddd"

For additional information about the syntax of regular expressions that you can use with the RegEx object, please see the documentation that comes with VBScript and make a search on MSDN Online.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist