VBA VBScript Regex Methods Regex

The VBA Regex supports 3 methods which are as follows:

  1. Execute
  2. Replace
  3. Text
  1. Execute

The execute method is used to extract a match from the given based on the defined matching Regex pattern. It returns an Object which retains all the matches, unlike an Array.

In the below program we have created a Function to collect all the matches found in the given string  which will match the regex Pattern string and will return an object holding all the matches found in the string (unlike an array). At last, we have looped through the returned object to read all the matches found.

Syntax

Program:

VBA VBScript Regex Methods Regex

2. Replace

The Replace method is used to search a character or string based on a defined pattern, and if the pattern is found, it is replaced with a new string or character as defined by the user (i.e the replacement string takes the place of each matched string). If the regex pattern is not matched with the string, the string remains unchanged. This method returns a new string with all the matched string replaced with provided string and replaces all strings that match a regular expression pattern with a specified replacement string.

In the below program, we have created a Function to replace all the matches found in each string, which matches the regex Pattern – with a given string. This program will return a new string where all the matches are replaced with the given string.

Syntax

Program:

VBA VBScript Regex Methods Regex

3. Test Method

This method is used to find whether a Regex pattern is matched in a given string. This method returns a Boolean value True if the match is found else it returns a Boolean False.

In the below example, we have created a function to check if the specified String matches with the given regex pattern and return Boolean – True or False based on the match found result.

Syntax

Program:

VBA VBScript Regex Methods Regex

Pin It on Pinterest

Share This