-
Notifications
You must be signed in to change notification settings - Fork 3
/
CRegExp.cls
155 lines (121 loc) · 3.64 KB
/
CRegExp.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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CRegExp"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'请一定注意以下这一点
'说明:请在“工程”-“引用”中,选择“Microsoft VBScript Regular Expressions 5.5”
Private mRegExp As New RegExp
Private mIgnoreCase As Boolean
Private mMultiLine As Boolean
Private mGlobal As Boolean
'Method
Public Sub Config(ByVal bIgnoreCase As Boolean, _
ByVal bMultiLine As Boolean, _
ByVal bGlobal As Boolean)
mIgnoreCase = bIgnoreCase
mMultiLine = bMultiLine
mGlobal = bGlobal
With mRegExp
.IgnoreCase = mIgnoreCase
.Global = mGlobal
.MultiLine = mMultiLine
End With
End Sub
Public Function GetMatchCollection(ByVal Patten As String, _
ByVal Sample As String) As MatchCollection
With mRegExp
.Pattern = Patten
Set GetMatchCollection = .Execute(Sample)
End With
End Function
Public Function GetSubMatches(ByVal Patten As String, _
ByVal Sample As String, _
Optional ByVal MatchIndex As Long = 0) As SubMatches
Dim Matches As MatchCollection
Set Matches = GetMatchCollection(Patten, Sample)
If Matches Is Nothing Then
Set GetSubMatches = Nothing
Else
Set GetSubMatches = Matches(MatchIndex).SubMatches
End If
End Function
Public Function GetScalarSubMatch(ByVal Patten As String, _
ByVal Sample As String) As String
Dim mSubMatches As SubMatches
Set mSubMatches = GetSubMatches(Patten, Sample)
'
If mSubMatches Is Nothing Then
GetScalarSubMatch = ""
Exit Function
End If
If mSubMatches.Count > 0 Then
GetScalarSubMatch = mSubMatches.Item(0)
Else
GetScalarSubMatch = ""
End If
End Function
Public Function GetSubMatchArray(ByVal Patten As String, _
ByVal Sample As String) As String()
Dim tSubMatches As SubMatches
Dim tArr() As String
Dim tCount As Long
Dim i As Long
Set tSubMatches = GetSubMatches(Patten, Sample)
If Not tSubMatches Is Nothing Then
tCount = tSubMatches.Count
Else
tCount = 0
End If
If tCount > 0 Then
ReDim tArr(tCount - 1)
For i = 0 To tCount - 1
tArr(i) = tSubMatches(i)
Next i
GetSubMatchArray = tArr
Else
ReDim tArr(0)
GetSubMatchArray = tArr
Exit Function
End If
End Function
Public Function GetAllSubMatchesArray(ByVal Patten As String, _
ByVal Sample As String) As String()
Dim tMC As MatchCollection
Dim tSM As SubMatches
Dim tC As New Collection
Dim tArr() As String
Dim tCount As Long
Dim i As Long
Dim j As Long
Dim tMCCount As Long
Set tMC = GetMatchCollection(Patten, Sample)
If tMC.Count > 0 Then
For i = 0 To tMC.Count - 1
Set tSM = tMC(i).SubMatches
If tSM.Count > 0 Then
For j = 0 To tSM.Count - 1
tC.Add tSM(j), CStr(tCount)
tCount = tCount + 1
Next j
End If
Next i
End If
If tMCCount > 0 Then
ReDim tArr(0)
Else
ReDim tArr(tCount - 1)
For i = 0 To tCount - 1
tArr(i) = tC.Item(CStr(i))
Next i
End If
GetAllSubMatchesArray = tArr
End Function