-
Notifications
You must be signed in to change notification settings - Fork 0
/
MRegExp.bas
202 lines (172 loc) · 5.12 KB
/
MRegExp.bas
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
Attribute VB_Name = "MRegExp"
Option Explicit
'
' Copyright (c) 2020,2022,2024 Koki Takeyama
'
' Permission is hereby granted, free of charge, to any person obtaining
' a copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included
' in all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
' IN THE SOFTWARE.
'
'
' Microsoft VBScript Regular Expression 5.5
' - VBScript_RegExp_55.RegExp
'
Private RegExpObject
'
' --- RegExp ---
'
'
' GetRegExp
' - Returns a RegExp object.
'
Public Function GetRegExp()
'Static RegExpObject
If IsEmpty(RegExpObject) Then
Set RegExpObject = CreateObject("VBScript.RegExp")
End If
Set GetRegExp = RegExpObject
End Function
'
' === RegExp ===
'
'
' RegExp_Execute
' - Executes a regular expression search against a specified string.
'
' RegExp_Replace
' - Replaces text found in a regular expression search.
'
' RegExp_Test
' - Executes a regular expression search against a specified string
' and returns a Boolean value that indicates if a pattern match was found.
'
'
' SourceString:
' Required. The text string upon which the regular expression is executed.
'
' ReplaceString:
' Required. The replacement text string.
'
' Pattern:
' Required. Regular string expression being searched for.
' https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/f97kw5ka(v=vs.84)
'
' IgnoreCase:
' Optional. The value is False if the search is case-sensitive,
' True if it is not. Default is False.
'
' GlobalMatch:
' Optional. The value is True if the search applies to the entire string,
' False if it does not. Default is False.
'
' MultiLine:
' Optional. The value is False if the search is single-line mode,
' True if it is multi-line mode. Default is False.
'
Public Function RegExp_Execute( _
SourceString, _
Pattern, _
IgnoreCase, _
GlobalMatch, _
MultiLine)
On Error Resume Next
With GetRegExp()
.Pattern = Pattern
.IgnoreCase = IgnoreCase
.Global = GlobalMatch
.MultiLine = MultiLine
Set RegExp_Execute = .Execute(SourceString)
End With
End Function
Public Function RegExp_Replace( _
SourceString, _
ReplaceString, _
Pattern, _
IgnoreCase, _
GlobalMatch, _
MultiLine)
On Error Resume Next
With GetRegExp()
.Pattern = Pattern
.IgnoreCase = IgnoreCase
.Global = GlobalMatch
.MultiLine = MultiLine
RegExp_Replace = .Replace(SourceString, ReplaceString)
End With
End Function
Public Function RegExp_Test( _
SourceString, _
Pattern, _
IgnoreCase, _
MultiLine)
On Error Resume Next
With GetRegExp()
.Pattern = Pattern
.IgnoreCase = IgnoreCase
.MultiLine = MultiLine
RegExp_Test = .Test(SourceString)
End With
End Function
Public Function RegExp_MatchedValue( _
SourceString, _
Pattern, _
IgnoreCase, _
MultiLine)
Dim Matches
Set Matches = _
RegExp_Execute( _
SourceString, Pattern, IgnoreCase, False, MultiLine)
If Matches Is Nothing Then
Exit Function
ElseIf Matches.Count = 0 Then
Exit Function
End If
RegExp_MatchedValue = Matches.Item(0).Value
End Function
Public Function RegExp_Matches_Count( _
SourceString, _
Pattern, _
IgnoreCase, _
MultiLine)
Dim Matches
Set Matches = _
RegExp_Execute( _
SourceString, Pattern, IgnoreCase, True, MultiLine)
If Matches Is Nothing Then
Exit Function
ElseIf Matches.Count = 0 Then
Exit Function
End If
RegExp_Matches_Count = Matches.Count
End Function
Public Function RegExp_LineNumber( _
SourceString, _
Index, _
LineSeparator)
Dim SourceStr
SourceStr = Left(SourceString, Index + 1)
Dim LineSep
Select Case LineSeparator
Case vbCr
LineSep = "\r"
Case vbLf
LineSep = "\n"
Case Else 'vbCrLf
LineSep = "\r\n"
End Select
RegExp_LineNumber = RegExp_Matches_Count(SourceStr, LineSep, False, False) + 1
End Function