Check the below script Hi that uses a regular expression to attempt to extract all legal email addresses from a string as an example:
Code:
Dim strPattern, arrAllMatches, strMatch, strMatches
strPattern = "([\w-\.]+)@\w{2,}(\.\w{2,}){1,5}(;([\w-\.]+)@\w{2,}(\.\w{2,}){1,5})*?"
strTestString = " envonfv ofnvfv fkdjfnv;djfn;dn email@domain.com.sdvzsd vj>ยจ^c zcln test@domain.com" & vbCrlf _
& " vsifdvjzs0v8djzs0fivdj zsofvn>anothermail@domain.com >| xidisdvnsv "
colAllMatches = fGetMatches(strPattern, strTestString)
For Each strMatch In colAllMatches
strMatches = strMatches & strMatch & vbCrlf
Next
MsgBox strMatches,, "Email Addresses found:"
Function fGetMatches(sPattern, sStr)
Dim regEx, retVal, sMatch, colMatches, temp
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = sPattern ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set colMatches = regEx.Execute(sStr) ' Execute search.
'# Convert Collection to Array
For Each sMatch In colMatches
temp = temp & sMatch & vbCrlf
Next
temp = Split(temp, vbCrlf)
fGetMatches = temp
End Function
Bookmarks