Regex VBA正则表达式
我正在尝试循环浏览电子邮件正文并将电子邮件转发到其他收件箱。我的正则表达式将任何4-6个字符的数字与前后的空格进行匹配,但问题是日期包含在电子邮件中,因此它也会选择4个字符的数字“2017”。什么是正则表达式,省略“2017”,只取所有其他4-6个字符的数字。这是我的密码Regex VBA正则表达式,regex,vba,Regex,Vba,我正在尝试循环浏览电子邮件正文并将电子邮件转发到其他收件箱。我的正则表达式将任何4-6个字符的数字与前后的空格进行匹配,但问题是日期包含在电子邮件中,因此它也会选择4个字符的数字“2017”。什么是正则表达式,省略“2017”,只取所有其他4-6个字符的数字。这是我的密码 Option Explicit Public Sub Forward(Item As Outlook.MailItem) Dim M1 As MatchCollection Dim M As Match
Option Explicit
Public Sub Forward(Item As Outlook.MailItem)
Dim M1 As MatchCollection
Dim M As Match
Dim Reg1 As Object
Dim myForward As Object
Set Reg1 = New RegExp
With Reg1
.Pattern = "(\s[0-9]{4,6}\s)"
.Global = True
End With
If Reg1.Test(Item.Body) Then
Set M1 = Reg1.Execute(Item.Body)
For Each M In M1
Debug.Print M.SubMatches(0) ' Immediate Window
'// allows for multiple matches in the message body
Item.Subject = M.SubMatches(0) & "; " & Item.Subject
Next
End If
Item.Save
Set myForward = Item.Forward
myForward.Recipients.Add "xxxxx@gmail.com"
myForward.Display
End Sub
这是我在转发的新电子邮件主题中的输出。SPORT-、2017 SPORT-、2017 SPORT-5556我只想能够捕获“SPORT-5556”您可以使用负前瞻(请参阅)从比赛中排除某些字符串 下面是我写这篇文章的方式:
Option Explicit
Public Sub Forward(Item As Outlook.MailItem)
Dim DigitsExp As New RegExp
Dim Matches As MatchCollection, Match As Match
If Item Is Nothing Then Exit Sub
DigitsExp.Pattern = "\s(?!2017\s)([0-9]{4,6})\s"
DigitsExp.Global = True
Set Matches = DigitsExp.Execute(Item.Body)
For Each Match in Matches
Debug.Print Match.SubMatches(0)
Item.Subject = Match.SubMatches(0) & "; " & Item.Subject
Next
If Not Item.Saved Then Item.Save
With Item.Forward
.Recipients.Add "xxxxx@gmail.com"
.Display
End With
End Sub
其中
\s(?!2017\s)([0-9]{4,6})\s
匹配任何不是2017
的4-6位子字符串,(?!2017\s)
是排除它的负前瞻。您可以使用负前瞻(请参阅)从匹配中排除某些字符串
下面是我写这篇文章的方式:
Option Explicit
Public Sub Forward(Item As Outlook.MailItem)
Dim DigitsExp As New RegExp
Dim Matches As MatchCollection, Match As Match
If Item Is Nothing Then Exit Sub
DigitsExp.Pattern = "\s(?!2017\s)([0-9]{4,6})\s"
DigitsExp.Global = True
Set Matches = DigitsExp.Execute(Item.Body)
For Each Match in Matches
Debug.Print Match.SubMatches(0)
Item.Subject = Match.SubMatches(0) & "; " & Item.Subject
Next
If Not Item.Saved Then Item.Save
With Item.Forward
.Recipients.Add "xxxxx@gmail.com"
.Display
End With
End Sub
其中,\s(?!2017\s)([0-9]{4,6})\s
匹配任何不是2017
的4-6位子字符串,(?!2017\s)
是排除它的负前瞻。说“编译错误:预期:=”说“编译错误:预期:=”