-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRegexStatic.cls
170 lines (140 loc) · 4.58 KB
/
RegexStatic.cls
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "RegexStatic"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Attribute VB_Description = "Static class (no need to initiate) wrapper for VBS Regular Expressions"
Attribute VB_Ext_KEY = "Rubberduck" ,"Predeclared Class Module"
'@ModuleDescription "Static class (no need to initiate) wrapper for VBS Regular Expressions"
'@Folder("vba-tools.Regex")
Option Explicit
'@PredeclaredId
#Const LateBinding = False
Private Type PrivateFields
#If LateBindig Then
Regex As Object
#Else
Regex As RegExp
#End If
Name As String
Global As Boolean
IgnoreCase As Boolean
Multiline As Boolean
End Type
Private this As PrivateFields
Private Sub Class_Initialize()
With this
#If LateBinding Then
Set .Regex = CreateObject("vbscript.regexp")
#Else
Set .Regex = New RegExp
#End If
'SET DEFAULTS
.Global = True
.IgnoreCase = True
.Multiline = True
End With
End Sub
Private Sub Class_Terminate()
Set this.Regex = Nothing
End Sub
Public Property Get Name() As String
Name = TypeName(this.Regex)
End Property
Public Property Get IsGlobal() As Boolean
IsGlobal = this.Global
End Property
Public Property Let IsGlobal(ByVal vNewValue As Boolean)
this.Global = vNewValue
End Property
Public Property Get IgnoreCase() As Boolean
IgnoreCase = this.IgnoreCase
End Property
Public Property Let IgnoreCase(ByVal vNewValue As Boolean)
this.IgnoreCase = vNewValue
End Property
Public Property Get Multiline() As Boolean
Multiline = this.Multiline
End Property
Public Property Let Multiline(ByVal vNewValue As Boolean)
this.Multiline = vNewValue
End Property
Public Sub QuickSetUp(ByVal IsGlobal As Boolean, ByVal Multiline As Boolean, ByVal IgnoreCase As Boolean)
With this
.Global = IsGlobal
.IgnoreCase = IgnoreCase
.Multiline = Multiline
End With
End Sub
Public Function GetMatch(ByRef srcString As String, ByRef matchPattern As String) As String
Dim matchObj As Object
Dim matchColl As Object
Dim matchStr As String
With this.Regex
.Global = this.Global
.Multiline = this.Multiline
.pattern = matchPattern
End With
Set matchColl = this.Regex.Execute(srcString)
If matchColl.Count > 0 Then
Set matchObj = matchColl.Item(0)
If matchObj.SubMatches.Count <> 0 Then
matchStr = matchObj.SubMatches(0)
Else
matchStr = matchObj.Value
End If
End If
GetMatch = matchStr
End Function
'@GetAllMatches - return Match Objects Collection
Public Function GetAllMatches(ByRef srcString As String, ByRef matchPattern As String) As Object
Dim matchObj As Object
Dim matchStr As String
With this
.Regex.Global = True 'Ignore class property to force global to get all matches
.Regex.IgnoreCase = this.IgnoreCase
.Regex.Multiline = this.Multiline
.Regex.pattern = matchPattern
End With
Set GetAllMatches = this.Regex.Execute(srcString)
End Function
'@Description ("GetNthMatch - returns match/submatch value from match collection by zero base match/submatch index.
'If there is no match/submatch for specific index returns vbNullString")
Public Function GetNthMatch(ByRef srcString As String, ByRef matchPattern As String, _
Optional ByVal matchIndex As Integer, Optional ByVal submatchIndex As Integer = -1) As String
Dim matchColl As Object
Set matchColl = GetAllMatches(srcString, matchPattern)
If matchColl.Count > matchIndex Then
Dim matchObj As Object
Set matchObj = matchColl.Item(matchIndex)
If submatchIndex = -1 And matchObj.SubMatches.Count = 0 Then
Dim matchStr As String
matchStr = matchObj.Value
ElseIf submatchIndex < 0 And matchObj.SubMatches.Count > 0 Then
matchStr = matchObj.SubMatches(0)
ElseIf matchObj.SubMatches.Count > submatchIndex Then
matchStr = matchObj.SubMatches(submatchIndex)
End If
End If
GetNthMatch = matchStr
End Function
Public Function TestMatch(ByRef srcString As String, ByRef testPattern As String) As Boolean
this.Regex.Global = this.Global
this.Regex.Multiline = this.Multiline
this.Regex.pattern = testPattern
TestMatch = this.Regex.Test(srcString)
End Function
Public Function ReplaceStr(ByRef srcString As String, ByRef matchString As String, ByVal replaceWith As String)
With this
.Regex.Global = .Global
.Regex.pattern = matchString
.Regex.IgnoreCase = .IgnoreCase
.Regex.Multiline = .Multiline
srcString = .Regex.Replace(srcString, replaceWith)
End With
ReplaceStr = srcString
End Function