Outlook 2007 中的正则表达式规则?
是否可以在 Outlook 2007 中基于正则表达式字符串创建规则?
我正在尝试为包含以下字符串的消息添加过滤器:4000-10
,一个四位数字,后跟一个破折号,然后是一个两位数字,可以是 中的任何数字0000-00 至
9999-99
。
我将其用作正则表达式: \b[0-9]{4}\-[0-9]{2}\b
但过滤器不起作用。我也尝试过其他一些修改,但没有成功。不过,我无法在网上找到任何关于 Outlook 是否支持在规则中输入正则表达式的具体信息,所以我想我应该在这里询问,以免浪费时间。
编辑:感谢下面克里斯的评论,我能够通过宏来实现这个过滤器。我想我会在下面分享我的代码,以防它能够帮助其他人:
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
Is it possible to create rules in Outlook 2007 based on a regex string?
I'm trying to add a filter for messages containing a string such as: 4000-10
, a four digit number followed by a dash and then a two digit number, which can be anything from 0000-00
to 9999-99
.
I was using this as a regex: \b[0-9]{4}\-[0-9]{2}\b
but the filter isn't working. I've tried a few other modifications as well with no luck. I wasn't able to find anything concrete online about whether Outlook even supports entering regexes into a rule, though, so I figured I would ask here in case I'm wasting my time.
EDIT: Thanks to Chris's comment below, I was able to implement this filter via a macro. I thought I would share my code below in case it is able to help anyone else:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道正则表达式是否可以直接在规则中使用,但是您可以让规则触发脚本,并且脚本可以使用正则表达式。我讨厌 Outlook。
首先,您必须通过“工具”-“宏”-“打开 Visual Basic 编辑器”(Alt-F11 是快捷方式)打开脚本编辑器。
编辑器将打开。它应该在左上角的小面板中包含项目大纲。该项目将被列为 VBAProject.OTM。展开此项以显示 Microsoft Office Outlook 对象。展开它以显示 ThisOutlookSession。双击 ThisOutlookSession 打开代码编辑窗格(可能是空白的)。
接下来选择“工具”菜单 |引用并启用名为“Microsoft VBScript Regular Expressions 5.5”之类的 RegExp 引用
您现在可以创建一个子例程来执行过滤操作。请注意,规则调用的子例程必须具有 Outlook.MailItem 类型的单个参数。例如:
接下来,您必须创建一个规则(工具 - 规则和警报)来触发此脚本。单击对话框上的“新建规则”按钮启动向导。选择规则的模板。从“从空白规则开始”类别中选择“邮件到达时检查”模板。单击“下一步”。
选择“仅在此计算机上”条件(很直观不是吗?),然后单击“下一步”。
选择“运行脚本”选项。在向导的底部显示您的新规则,它应该显示:
短语“脚本”是一个可单击的链接。单击它,Outlook 将显示一个对话框,其中应列出您之前创建的子例程。选择您的子例程并单击“确定”按钮。
您可以单击“下一步”向规则添加例外,或者如果没有例外,则单击“完成”。
现在,好像该过程还不够复杂,每次您停止并重新启动 Outlook 时,该规则都会失效,除非您使用代码签名密钥对脚本进行签名。
如果您还没有代码签名密钥,可以使用 创建一个 href="http://www.openssl.org/lated/binaries.html" rel="noreferrer">OpenSSL。
我是否提到过我讨厌 Outlook?
I do not know if a regex can be used directly in a rule, but you can have a rule trigger a script and the script can use regexes. I hate Outlook.
First, you have to open the script editor via Tools - Macro - Open Visual Basic Editor (Alt-F11 is the shortcut).
The editor will open. It should contain a project outline in a small panel in the top-left corner. The project will be listed as VBAProject.OTM. Expand this item to reveal Microsoft Office Outlook Objects. Expand that to reveal ThisOutlookSession. Double-click ThisOutlookSession to open the code editing pane (which will probably be blank).
Next select Tools menu | References and enable the RegExp references called something like "Microsoft VBScript Regular Expressions 5.5"
You can now create a subroutine to perform your filtering action. Note that a subroutine called by a rule must have a single parameter of type Outlook.MailItem. For example:
Next, you have to create a rule (Tools - Rules and Alerts) to trigger this script. Click the New Rule button on the dialog to launch the wizard. Select a template for the rule. Choose the "Check messages when they arrive" template from the "Start from a blank rule" category. Click Next.
Choose the "On this machine only" condition (intuitive isn't it?) and click next.
Choose the "run a script" option. At the bottom of the wizard where it shows your new rule, it should read:
The phrase "a script" is a clickable link. Click it and Outlook will display a dialog that should list the subroutine you created earlier. Select your subroutine and click the OK button.
You can click Next to add exceptions to the rule or click Finish if you have no exceptions.
Now, as though that process was not convoluted enough, this rule will deactivate every time you stop and restart Outlook unless you sign the script with a code signing key.
If you don't already have a code signing key, you can create one with OpenSSL.
Did I mention that I hate Outlook?
Microsoft Outlook 不支持正则表达式。您可以执行通配符搜索,尽管由于某些无法解释的原因通配符是
%
,而不是*
。Microsoft Outlook does not support regular expressions. You can perform wildcard searches, although for some inexplicable reason the wildcard character is
%
, not*
.