我正在解析电子邮件正文以提取长度为32个字符的字符串(MD5哈希值)。我也会在超过32个字符的字符串上进行匹配,我无法调整正则表达式以使其正常工作。
Sub GetValueUsingRegEx()
Dim olMail As Outlook.MailItem
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Dim pat As String
Set olMail = Application.ActiveExplorer().Selection(1)
'Debug.Print olMail.Body
pat = "[a-fA-F0-9]{32}"
Call parseTheEmail(pat, olMail)
End Sub
Sub parseTheEmail(ByVal pat As String, ByVal email As Outlook.MailItem)
Set Reg1 = New RegExp
With Reg1
.pattern = pat
.Global = True
.IgnoreCase = True
End With
If Reg1.Test(email.Body) Then
Set M1 = Reg1.Execute(email.Body)
For Each M In M1
Debug.Print M
Next
End If
End Sub
答案 0 :(得分:1)
你需要一个单词边界:
pat = "\b[a-fA-F0-9]{32}\b"
或者如果它单独出现在一个单独的行上,那么您可以使用:
pat = "^[a-fA-F0-9]{32}$"
并将正则表达式.MultiLine
属性设置为True