Micron,
Just saw this post. I don't know Regex all that well --I have used it on occasion but thought I'd try some vba.
Did this based on your post. Hope it's useful. If I have misunderstood the expected result and patterns, let me know.
A subroutine to look for pattern(s)
Pattern1 (####)
Pattern 2 (###)
Code:
'---------------------------------------------------------------------------------------
' Procedure : MicronRegEx
' Author : mellon
' Date : 17-Jan-2018
' Purpose : To see if input string contain 1 of 2 possible patterns and
'identify the pattern matches.
'---------------------------------------------------------------------------------------
'
Sub MicronRegEx(s As String)
Dim p1 As String
Dim p2 As String
10 On Error GoTo MicronRegEx_Error
20 p1 = Right(s, 6) '(####)
30 p2 = Right(s, 5) '(###)
40 If p1 Like "(####)" Then
50 Debug.Print "found P1 in s " & p1 & " " & s
60 ElseIf p2 Like "(###)" Then
70 Debug.Print "found P2 in s " & p2 & " " & s
80 End If
MicronRegEx_Exit:
90 Exit Sub
MicronRegEx_Error:
100 MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure MicronRegEx of Module AWF_Related"
110 Resume MicronRegEx_Exit
End Sub
A test routine
Code:
'---------------------------------------------------------------------------------------
' Procedure : testMicron
' Author : mellon
' Date : 17-Jan-2018
' Purpose :Test routine t exercise the MicronRegEx routine
'---------------------------------------------------------------------------------------
'
Sub testMicron()
Dim strX(4) As String
10 On Error GoTo testMicron_Error
20 strX(0) = "123b7(56)abc)"
30 strX(1) = "12(3b7356)abc)"
40 strX(2) = "123b7(56)a(7895)"
50 strX(3) = "123b7(564)ab((784)"
60 strX(4) = "123b7(56)abc*(888888)"
Dim i As Integer
70 For i = 0 To 4
80 MicronRegEx (strX(i))
90 Next i
testMicron_Exit:
100 Exit Sub
testMicron_Error:
110 MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure testMicron of Module AWF_Related"
120 Resume testMicron_Exit
End Sub
Update: After submitting the post I did find this to work with ~~minimal testing with Rubular
http://rubular.com/
[(]\d{3,4}[)]\z
Interpretation:
[(] ----the ( character
\d{3,4}--- followed by 3 or 4 digits
[)] -----followed by the ) character
\z ------at the end of the string
Note also: I have seen where \z was not accepted by some implementations. you may have to use a $
eg: [(]\d{3,4}[)]$
Another update:
I modified the test to use a vba function with regex, the $ was the issue with my regex attempts.
Here is the revised test using a function EmailFinder
Code:
'revised test
Sub testMicron()
Dim strX(4) As String
10 On Error GoTo testMicron_Error
20 strX(0) = "123b7(56)abc)"
30 strX(1) = "12(3b7356)abc)"
40 strX(2) = "123b7(56)a(7895)"
50 strX(3) = "123b7(564)ab((784)"
60 strX(4) = "123b7(56)abc*(888888)"
Dim i As Integer
'80 For i = 0 To 4
'90 MicronRegEx (strX(i))
'100 Next i
70 For i = 0 To 4
80 Debug.Print strX(i) & "---" & Emailfinder(strX(i))
90 Next i
testMicron_Exit:
100 Exit Sub
testMicron_Error:
110 MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure testMicron of Module AWF_Related"
120 Resume testMicron_Exit
End Sub
Code:
Function Emailfinder(t As String) As String
Dim MyRE As Object
On Error GoTo Emailfinder_Error
10 Set MyRE = New regexp
Dim MyMatches As MatchCollection
Dim MyResult As String
'set the email pattern
20
30 Emailfinder = "" 'set to empty string
40 MyRE.pattern = "[(]\d{3,4}[)]$" ' <<<<<< I changed the pattern in this existing function for this test
50 MyRE.Global = True
60 MyRE.IgnoreCase = True
70 Set MyMatches = MyRE.Execute(t)
80 If MyMatches.Count > 0 Then
90 For Each mymatch In MyMatches
100 MyResult = MyResult & mymatch.value & vbCrLf
110 Next
120 Emailfinder = MyResult ' one or more valid email addresses
130 Else
140 Emailfinder = "" 'empty string
150 End If
On Error GoTo 0
Exit Function
Emailfinder_Error:
MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure Emailfinder of Module Module1"
End Function
Output from latest tests:
123b7(56)abc)---
12(3b7356)abc)---
123b7(56)a(7895)---(7895)
123b7(564)ab((784)---(784)
123b7(56)abc*(888888)---