I created a function that will pull email addresses out of an input string. It uses regular expressions (regex) which is based on pattern matching. I am including it here, along with a test routine I created that uses some of your test data with a few adjustments. I've added extra email addresses separated by spaces within some of the test data records.
If you decide to use (or adjust to your needs) the function, you need to have a reference to the Microsoft VBScript Regular Expression library. Regex is great for pattern matching, but there is nothing wrong with the Mid(),InStr(),InStrRev(),Left(),Right(), Trim() functions either.
I could not find a good example using Access vba where there was a search and extract of matches to a pattern. So this will serve as a sample.
Code:
'---------------------------------------------------------------------------------------
' Procedure : Emailfinder
' Author : Jack
' Date : 18/02/2014
' Purpose : This will get valid email addresses from a atring. [Valid == matches the pattern]
' Found "best regex pattern for email validation" at
' http://blog.trojanhunter.com/2012/09/26/the-best-regex-to-validate-an-email-address
' Pattern: [-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}
'
' This routine will return multiple email addresses when there are multiples in the string.
'
'' ---------------------------------------
'*** Requires a reference to the Microsoft VBScript Regular Expressions library
' _______________________________________
'---------------------------------------------------------------------------------------
'
Function Emailfinder(T As String) As String
Dim MyRE As Object
10 Set MyRE = New Regexp
Dim MyMatches As MatchCollection
Dim MyResult As String
'set the email pattern
20 On Error GoTo Emailfinder_Error
30 Emailfinder = "" 'set to empty string
40 MyRE.Pattern = "[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}"
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
160 On Error GoTo 0
170 Exit Function
Emailfinder_Error:
180 MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure Emailfinder of Module Module1"
End Function
Here is the test routine I used that includes some of your test data.
Code:
Sub sometest()
Dim s(14) As String 'test data
Dim i As Integer
On Error GoTo sometest_Error
s(0) = "h="
s(1) = " 1="
s(2) = " 10pt;>.</span></p>=jim.XX@samoa.net.au<p= ==mmgf hank@gmail.com gerry@Att.net"
s(3) = "(301)277-="
s(4) = " <test@oceanfire.com;bill@gmail.com>"
s(5) = "<test@oceanfire.com>;"
s(6) = "Test@ promotioncorner.com"
s(7) = "<test@oceanfire.com>"
s(8) = "<20140206130446.BB05F2995@diligence.cnchost.com>"
s(9) = "<test@promotioncorner.com>"
s(10) = "<test@wadecorporategifts.com>"
s(11) = "<0Lh8Cl-1VPCF507ZX-00oYM5@mx.perfora.net>"
s(12) = "<test@vectorpromo.com>"
s(13) = "-0500"
s(14) = " Test@ promotioncorner.com"
For i = 0 To UBound(s)
If Emailfinder(s(i)) <> "" Then
Debug.Print Emailfinder(s(i)) & " " & i
End If
Next i
On Error GoTo 0
Exit Sub
sometest_Error:
MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure sometest of Module Module1"
End Sub
Output"
Code:
jim.XX@samoa.net.au
hank@gmail.com
gerry@Att.net
2
test@oceanfire.com
bill@gmail.com
4
test@oceanfire.com
5
test@oceanfire.com
7
20140206130446.BB05F2995@diligence.cnchost.com
8
test@promotioncorner.com
9
test@wadecorporategifts.com
10
0Lh8Cl-1VPCF507ZX-00oYM5@mx.perfora.net
11
test@vectorpromo.com
12
Good luck with your project