Extract Email address from a table in .HTMLbody

我的未来我决定 提交于 2019-12-01 22:18:19

Have you looked in to Regular Expressions in VBA, I haven't worked on it in while but here is an example.


Option Explicit
Sub Example()
    Dim Item As MailItem
    Dim RegExp As Object
    Dim Search_Email As String
    Dim Pattern As String     
    Dim Matches As Variant

    Set RegExp = CreateObject("VbScript.RegExp")

    Pattern = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"

    For Each Item In ActiveExplorer.Selection

        Search_Email = Item.body

        With RegExp
            .Global = False
            .Pattern = Pattern
            .IgnoreCase = True
            Set Matches = .Execute(Search_Email)
        End With

        If Matches.Count > 0 Then
            Debug.Print Matches(0)
        Else
            Debug.Print "Not Found "
        End If

    Next

    Set RegExp = Nothing

End Sub

Or Pattern = "(\S*@\w+\.\w+)" Or "(\w+(?:\W+\w+)*@\w+\.\w+)"


Regular-expressions.info/tutorial

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b Simple pattern that describes an email address.

A series of letters, digits, dots, underscores, percentage signs and hyphens, followed by an at sign, followed by another series of letters, digits and hyphens, finally followed by a single dot and two or more letters

[A-Z0-9._%+-]+ Match a single character present in the list below

A-Z A single character in the range between A and Z (case sensitive)

0-9 A single character in the range between 0 and 9

._%+- A single character in the list

@ Matches the character @ literally


Quantifiers

Udemy.com/vba-regex/

+---------+---------------------------------------------+------------------------------------------------------------+
| Pattern |                   Meaning                   |                          Example                           |
+---------+---------------------------------------------+------------------------------------------------------------+
|         |                                             |                                                            |
| –       | Stands for  a range                         | a-z means all the letters a to z                           |
| []      | Stands for any one of the characters quoted | [abc] means either a, b or c.[A-Z] means either A, B, …, Z |
| ()      | Used for grouping purposes                  |                                                            |
| |       | Meaning is ‘or’                             | X|Y, means X or Y                                          |
| +       | Matches the character one or more times     | zo+ matches ‘zoo’, but not ‘z’                             |
| *       | Matches the character zero or more times    | “lo*” matches either “l” or “loo”                          |
| ?       | Matches the character zero or once          | “b?ve?” matches the “ve” in “never”.                       |
+---------+---------------------------------------------+------------------------------------------------------------+

Wikibooks.org/wiki/Visual_Basic/Regular_Expressions

https://regex101.com/r/oP2yR0/1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!