-
Notifications
You must be signed in to change notification settings - Fork 2
RegExp2
Laurent Franceschetti edited this page Sep 15, 2018
·
5 revisions
There is indeed a standard library, but unfortunately it was written in the spirit of a system library: to be as general as possible... which is often the opposite as user friendly, since a user has a particular use case. That is why it needs a wrapper for daily use in vba.
Hint: to develop your regular expression, you could use an online tool, e.g. : https://regex101.com/
- Libraries: Microsoft VB Script Regular Expressions
See the module source.
Here is the signature:
Function RegMatch(ByVal sourceString As String, Pattern As String, _
Optional IgnoreCase = True, Optional Multiline = False) As String
a = RegMatch("Hello world", "world")
Debug.Print a
b = RegMatch("Hello world", "^\w*")
Debug.Print b
Will return:
world
Hello
Function RegReplace(sourceString As String, Pattern As String, ReplaceVar As String, _
Optional IgnoreCase = True, Optional GlobalReplace = True, Optional Multiline = False) As String
Function RegMatches(sourceString As String, Pattern As String, _
Optional IgnoreCase = True, Optional Multiline = False) As MatchCollection
The result is a collection, which is also indexed.
In the Immediate window:
Debug.Print Regmatches("Hello world this is a wonderful.", "wo\w*")(1)
wonderful
Here is an example from the source code:
Sub TestRegMatches(sourceString As String, Pattern As String)
' Test Regmatches; you can use it as example, or a debug tool for your regular expressions.
Dim myMatch As Match
For Each myMatch In RegMatches(sourceString, Pattern)
Debug.Print "Found " & myMatch.Value
Next
End Sub
vba_db is a complete VBA toolkit for accessing local and remote databases, creating elaborate queries and manipulating result sets as tables (lists of dictionaries).
Available under MIT license.