代码之家  ›  专栏  ›  技术社区  ›  Kevin Coppock

outlook2007中的正则表达式规则?

  •  45
  • Kevin Coppock  · 技术社区  · 15 年前

    我正在尝试为包含以下字符串的邮件添加筛选器: 4000-10 一个数字后面可以是一个数字,然后是一个数字后面跟一个数字 0000-00 9999-99

    我用这个作为正则表达式: \b[0-9]{4}\-[0-9]{2}\b 但是过滤器坏了。我也尝试了一些其他的修改,但没有成功。不过,我在网上找不到任何关于Outlook是否支持在规则中输入regex的具体信息,所以我想我应该在这里问一下,以防浪费时间。

    编辑:多亏了克里斯下面的评论,我能够通过一个宏实现这个过滤器。我想我会在下面分享我的代码,以防它能帮助其他人:

    Sub JobNumberFilter(Message As Outlook.MailItem)
        Dim MatchesSubject, MatchesBody
        Dim RegEx As New RegExp
    
        'e.g. 1000-10'
        RegEx.Pattern = "([0-9]{4}-[0-9]{2})"
    
        'Check for pattern in subject and body'
        If (RegEx.Test(Message.Subject) Or RegEx.Test(Message.Body)) Then
            Set MatchesSubject = RegEx.Execute(Message.Subject)
            Set MatchesBody = RegEx.Execute(Message.Body)
            If Not (MatchesSubject Is Nothing And MatchesBody Is Nothing) Then
                'Assign "Job Number" category'
                Message.Categories = "Job Number"
                Message.Save
            End If
        End If
    End Sub
    
    2 回复  |  直到 6 年前
        1
  •  51
  •   lessthanideal Chris Judge    10 年前

    我不知道正则表达式是否可以直接在规则中使用,但是你可以让一个规则触发一个脚本,这个脚本可以使用正则表达式。我讨厌前景。

    编辑器将打开。它应该在左上角的一个小面板中包含一个项目大纲。项目将列为vba项目.OTM. 展开此项目以显示Microsoft Office Outlook对象。展开它来显示这个outlooksession。双击ThisOutlookSession以打开“代码编辑”窗格(可能为空)。

    接下来,选择“工具”菜单|引用并启用名为“Microsoft VBScript Regular Expressions 5.5”的RegExp引用

    ' note that Stack Overflow's syntax highlighting doesn't understand VBScript's
    ' comment character (the single quote) - it treats it as a string delimiter.  To
    ' make the code appear correctly, each comment must be closed with another single
    ' quote so that the syntax highlighter will stop coloring everything as a string.'
    
    Public Enum Actions
        ACT_DELIVER = 0
        ACT_DELETE = 1
        ACT_QUARANTINE = 2
    End Enum
    
    Sub MyNiftyFilter(Item As Outlook.MailItem)
        Dim Matches, Match
        Dim RegEx As New RegExp
        RegEx.IgnoreCase = True
    
        ' assume mail is good'
        Dim Message As String: Message = ""
        Dim Action As Actions: Action = ACT_DELIVER
    
        ' SPAM TEST: Illegal word in subject'
        RegEx.Pattern = "(v\|agra|erection|penis|boner|pharmacy|painkiller|vicodin|valium|adderol|sex med|pills|pilules|viagra|cialis|levitra|rolex|diploma)"
        If Action = ACT_DELIVER Then
            If RegEx.Test(Item.Subject) Then
                Action = ACT_QUARANTINE
                Set Matches = RegEx.Execute(Item.Subject)
                Message = "SPAM: Subject contains restricted word(s): " & JoinMatches(Matches, ",")
            End If
        End If
    
        ' other tests'
    
        Select Case Action
            Case Actions.ACT_QUARANTINE
                Dim ns As Outlook.NameSpace
                Set ns = Application.GetNamespace("MAPI")
    
                Dim junk As Outlook.Folder
                Set junk = ns.GetDefaultFolder(olFolderJunk)
    
                Item.Subject = "SPAM: " & Item.Subject
                If Item.BodyFormat = olFormatHTML Then
                    Item.HTMLBody = "<h2>" & Message & "</h2>" & Item.HTMLBody
                Else
                    Item.Body = Message & vbCrLf & vbCrLf & Item.Body
                End If
    
                Item.Save
                Item.Move junk
    
            Case Actions.ACT_DELETE
                ' similar to above, but grab Deleted Items folder as destination of move'
    
            Case Actions.ACT_DELIVER
                ' do nothing'
        End Select
    End Sub
    
    
    Private Function JoinMatches(Matches, Delimeter)
        Dim RVal: RVal = ""
    
        For Each Match In Matches
            If Len(RVal) <> 0 Then
                RVal = RVal & ", " & Match.Value
            Else
                RVal = RVal & Match.Value
            End If
        Next
    
        JoinMatches = RVal
    End Function
    

    接下来,您必须创建一个规则(工具-规则和警报)来触发此脚本。单击对话框上的“新建规则”按钮启动向导。为规则选择模板。从“从空白规则开始”类别中选择“邮件到达时检查”模板。单击“下一步”。

    选择“运行脚本”选项。在向导底部显示新规则的位置,它应该是:

    Apply this rule after the message arrives
    on this machine only
    run a script
    

    短语“脚本”是一个可点击的链接。单击它,Outlook将显示一个对话框,其中应列出您先前创建的子例程。选择子例程并单击“确定”按钮。

    可以单击“下一步”向规则添加异常,如果没有异常,则单击“完成”。

    现在,好像这个过程还不够复杂,每次您停止并重新启动Outlook时,此规则都将停用,除非您使用代码签名密钥对脚本进行签名。

    如果您还没有代码签名密钥,则可以 create one 具有 OpenSSL .

        2
  •  17
  •   Ether    15 年前

    Microsoft Outlook does not support regular expressions . 您可以执行通配符搜索,尽管出于某种莫名其妙的原因,通配符是 % * .