The following code snippet will show you how to use regular expressions in VBA.
Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
' @Author - Alexander Bolte' @Change Date - 2014-02-28' @Description - Searches given a string for a given regular expression and returns the result based on parametrization in a collection.' @Param findAll - True, if all occurences matching the given pattern should be returned. False, if only the first occurence matching a given pattern should be returned.' @Returns - A collection of type VBA.Collection holding all found matches as Strings.FunctiongetRegExMatches(ByValpatrnAsString,ByValstrngAsString,OptionalByValfindAllAsBoolean=True,OptionalByValuseIgnoreCaseAsBoolean=False)AsVBA.CollectionDimregExAsRegExpDimmatchesAsMatchCollectionDimretAsNewVBA.CollectionDimiAsIntegerSetregEx=NewVBScript_RegExp_55.RegExpregEx.Pattern=patrn' Set pattern.regEx.IgnoreCase=useIgnoreCase' Set case insensitivity / sensitivity.regEx.Global=findAll' Set global applicability. If true, find all occurences, if false, find only fisrt oneregEx.MultiLine=True' This parameter controls, if a pattern should be searched accross line breaks or not.IfregEx.Test(strng)ThenSetmatches=regEx.Execute(strng)' Execute search.Fori=0Tomatches.Count-1ret.Addmatches.Item(0).ValueNextiEndIfSetmatches=NothingSetregEx=NothingSetgetRegExMatches=retEndFunction
Referenced APIs
This code references the folowing API, which is a VBScript API.
And here a link on regular expressions in general. I found it very helpful, as I would not consider myself someone who fully understands regular expressions.
I just like to use them as they make my life much easier.