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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as