Regular Expressions
Regular expressions (often called regex or regexp) are patterns used to match, search and manipulate text.
They provide a compact way to describe text structures such as email addresses, dates, numbers, words, or any other textual pattern.
link - regexone.com
VBA Built-in Classes
Starting with Office 365, Version 2508, There are four built-in classes that can handle regular expressions.
RegExp - The main regular expression engine (pattern, options, execution).
Match - Represents a single match result.
MatchCollection - A collection of all matches returned by a RegExp execution.
SubMatches - A collection of captured groups (parentheses) within a match.
These are the same objects as the VBScript libray except they are built-in to VBA so you do not need any external reference.
Microsoft VBScript Regular Expressions 5.5
In earlier versions you had to add a reference to an additional library.
link - devblogs.microsoft.com/microsoft365dev/how-to-prepare-vba-projects-for-vbscript-deprecation/
link - learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/ms974570(v=msdn.10)
Code Snippets
Matching Exact 2 Characters
Matching Any 3 Digits
The "\d" can match any digit, equivalent to [0-9]
Matching Any 3 Characters
The "." will match any letter, digit, whitespace, everything except \n
Matching Any 3 Non Digits
The "\D" will match any non digit, equivalent to [^0-9]
Matching A Full Stop
The "\." will match a full stop
Matching Certain Characters
The "[et]" will match single "e" and "t" characters
Matching Excluding Characters
The "[^et]" will match everything that doesn't contain the single characters "e" and "t"
Matching Any Alphanumeric Characters
The "\w" will match all letters and numbers, equivalent to [A-Za-z0-9]
Matching Any 3 Letters with the first one in uppercase
Matching Repetition
This will match "aaaa"
This will match ??
This will match any characters repeated 2,3 or 4 times
Matching Zero or More Repetitions
The "*" will match zero or more repetitions
This will match any number of a's, b's and c's
Matching One or More Repetitions
The "+" will match one or more repetitions
Matching Optional Characters
The "?" will match optional characters
This will make the "b" an optional character
Matching Whitespace
The "\s" will match any single space, tab character, new line or carriage return character
Matching Start and End
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext