forked from Vitosh/VBA_personal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegEx.vb
92 lines (65 loc) · 2.72 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
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
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
'===============================================================================
'===============================================================================
'===============================================================================
Public Function findTheSubString(wholeString As String, subString As String) As String
Dim regEx As Object
Dim inputMatches As Object
Dim regExString As String
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.Pattern = Split(subString, "*")(0) & "[\s\S]*" & Split(subString, "*")(1)
.IgnoreCase = True
.Global = True
Set inputMatches = .Execute(wholeString)
If regEx.test(wholeString) Then
findTheSubString = inputMatches(0)
Else
findTheSubString = "Not Found!"
End If
End With
End Function
'===============================================================================
'===============================================================================
'===============================================================================