-
Notifications
You must be signed in to change notification settings - Fork 59
/
LIB_Regex.bas
176 lines (144 loc) · 6.68 KB
/
LIB_Regex.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
Attribute VB_Name = "LIB_Regex"
'---------------------------------------------------------------------------------------------------------------------------------------------
'
' Regex Library v0.1
'
' + References dependencies
' - Microsoft VBScript Regular Expressions 1.0
'
'
' Functions lists
' ---------------
'
' + Function matchExpreg(ByVal txt As String, ByVal matchPattern As String, ByVal replacePattern As String) As String
' * Description : Match the specified pattern in the text given in argument and apply the replacementPattern
' * Specifications / limitations
' - Multiline
' - Not case sensitive
' * Arguments
' - ByVal txt As String : the text to search in
' - ByVal matchPattern As String : the regular expression pattern
' - ByVal replacePattern As String : the replacement pattern
' + Function findExpreg(ByVal cellContent As Range, ByVal cellPattern As Range) As String
' * Description : Return the first occurence of the regular expression pattern found in the given expression
' * Specifications / limitations
' - Multiline
' - Not case sensitive
' * Arguments
' - ByVal txt As String : the text to search in
' - ByVal matchPattern As String : the regular expression pattern
' + Function stripTags(ByVal txt As String) As String
' * Description : Strips all the tags within a given string
' * Specifications / limitations
' - None
' * Arguments
' - ByVal txt As String : the text to search in'
'
'
' Revisions history
' -----------------
' - Emile Fyon 11/07/2012 v0.1 Creation
'
'---------------------------------------------------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------------------------------------------------
' + Function matchExpreg(ByVal txt As String, ByVal matchPattern As String, ByVal replacePattern As String) As String
' * Description : Match the specified pattern in the text given in argument and apply the replacementPattern
' * Specifications / limitations
' - Multiline
' - Not case sensitive
' * Arguments
' - ByVal txt As String : the text to search in
' - ByVal matchPattern As String : the regular expression pattern
' - ByVal replacePattern As String : the replacement pattern
'
'
' Last edition date : 11/07/2012
'
' Revisions history
' -----------------
' - Emile Fyon 11/07/2012 Creation
'
'---------------------------------------------------------------------------------------------------------------------------------------------
Function matchExpreg(ByVal txt As String, ByVal matchPattern As String, ByVal replacePattern As String) As String
Dim RE As Object, REMatches As Object
' Set cell = Range("e15")
' strData = cell.Value
Dim reg_exp As New RegExp
reg_exp.pattern = matchPattern
reg_exp.IgnoreCase = True
reg_exp.Global = True
txt = reg_exp.Replace(txt, replacePattern)
matchExpreg = txt
End Function
'---------------------------------------------------------------------------------------------------------------------------------------------
' + Function findExpreg(ByVal cellContent As Range, ByVal cellPattern As Range) As String
' * Description : Return the first occurence of the regular expression pattern found in the given expression
' * Specifications / limitations
' - Multiline
' - Not case sensitive
' * Arguments
' - ByVal txt As String : the text to search in
' - ByVal matchPattern As String : the regular expression pattern
'
'
' Last edition date : 11/07/2012
'
' Revisions history
' -----------------
' - Emile Fyon 11/07/2012 Creation
' - Emile Fyon 27/08/2012 Handle response in case the string is not found (return False)
'
'---------------------------------------------------------------------------------------------------------------------------------------------
Function findExpreg(ByVal txt As String, ByVal matchPattern As String) As String
On Error GoTo errorHandler
Dim expReg As New RegExp
expReg.pattern = matchPattern
expReg.IgnoreCase = True
expReg.Global = True
Set res = expReg.Execute(txt)
txt = res(0).submatches(0)
findExpreg = txt
Exit Function
errorHandler:
findExpreg = False
End Function
'---------------------------------------------------------------------------------------------------------------------------------------------
' + Function stripTags(ByVal txt As String) As String
' * Description : Strips all the tags within a given string
' * Specifications / limitations
' - None
' * Arguments
' - ByVal txt As String : the text to search in
'
'
' Last edition date : 11/07/2012
'
' Revisions history
' -----------------
' - Emile Fyon 11/07/2012 Creation
'
'---------------------------------------------------------------------------------------------------------------------------------------------
Function stripTags(ByVal txt As String) As String
regMask = "(<.+?>)"
stripTags = matchExpreg(txt, regMask, "")
End Function
'---------------------------------------------------------------------------------------------------------------------------------------------
' + Function fileNameFromFullPath(ByVal txt As String) As String
' * Description : Extract the filename of a file from his full path
' * Specifications / limitations
' - None
' * Arguments
' - ByVal txt As String : the text to search in
'
'
' Last edition date : 02/09/2012
'
' Revisions history
' -----------------
' - Emile Fyon 02/09/2012 Creation
'
'---------------------------------------------------------------------------------------------------------------------------------------------
Function fileNameFromFullPath(ByVal txt As String) As String
regMask = ".+\\(.+)"
fileNameFromFullPath = findExpreg(txt, regMask)
End Function