Created
February 21, 2023 06:27
-
-
Save aruku7230/9ab9a8ae774d4f44ae26686db1c1f1d1 to your computer and use it in GitHub Desktop.
Use Regular Expression by VBA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Public Function RegExpTest(text As String, pattern As String, Optional match_case As Boolean = True) As Boolean | |
Dim regex As Object | |
On Error GoTo ErrHandl | |
Set regex = CreateObject("VBScript.RegExp") | |
regex.pattern = pattern | |
regex.Global = True | |
regex.MultiLine = True | |
If True = match_case Then | |
regex.ignorecase = False | |
Else | |
regex.ignorecase = True | |
End If | |
RegExpTest = regex.Test(text) | |
Exit Function | |
ErrHandl: | |
RegExpTest = CVErr(xlErrValue) | |
End Function | |
Public Function RegExpReplace(text As String, pattern As String, text_replace As String, Optional match_case As Boolean = True) As String | |
Dim regex As Object | |
On Error GoTo ErrHandle | |
Set regex = CreateObject("VBScript.RegExp") | |
regex.pattern = pattern | |
regex.Global = True | |
regex.MultiLine = True | |
If True = match_case Then | |
regex.ignorecase = False | |
Else | |
regex.ignorecase = True | |
End If | |
RegExpReplace = regex.Replace(text, text_replace) | |
Exit Function | |
ErrHandle: | |
RegExpReplace = CVErr(xlErrValue) | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment