forked from AllenMattson/VBA_personal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegEx.vb
65 lines (45 loc) · 1.87 KB
/
RegEx.vb
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
Option Explicit
Public Sub RegExExample()
Dim strString As String
Dim lngCounter As Long
Dim objRegex As Object
Dim arrWords As Variant
'RegEx with late binding
Set objRegex = CreateObject("VBScript.RegExp")
strString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
arrWords = Split(strString)
objRegex.Pattern = "or"
For lngCounter = LBound(arrWords) To UBound(arrWords)
If objRegex.test(arrWords(lngCounter)) Then
Debug.Print arrWords(lngCounter)
End If
Next lngCounter
End Sub
'===============================================================================
'===============================================================================
'removes anything that is not a digit or word from the string===================
Public Function removeInvisibleThings(s As String) As String
Dim regEx As Object
Dim inputMatches As Object
Dim regExString As String
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.pattern = "[^a-zA-Z0-9]"
.IgnoreCase = True
.Global = True
Set inputMatches = .Execute(s)
If regEx.test(s) Then
removeInvisibleThings = .Replace(s, vbNullString)
Else
removeInvisibleThings = s
End If
End With
End Function
Public Sub TestMe()
Debug.Print removeInvisibleThings("aa1 Abc 67 ( *^ 45 ")
Debug.Print removeInvisibleThings("aa1 ???!")
Debug.Print removeInvisibleThings(" aa1 Abc 1267 ( *^ 45 ")
End Sub
'===============================================================================
'===============================================================================
'===============================================================================