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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS