我之前在此链接上提出了一个关于正则表达式的问题 How to delete certain sentences between 2 words before forwarding the email?
我回去尝试了这个建议并做了一些观察。 以下是我尝试的脚本:
Set FwdMsg = item.Forward
With FwdMsg
Dim regEx As New RegExp
With regEx
.Global = False
.multiline = True
.ignorecase = False
.pattern = strPattern
End With
Dim newbody As String
Dim source As String
source = FwdMsg.HTMLBody
Dim replacestr As String
replacestr = "$1\n\nplease call me with this number\n\n$2"
strPattern = "^(Impact:)\s*(?:(?!^(?:Impact:|Correction[^\S\n]+Action))[\s\S])*^(Correction[^\S\n]+Action)"
newbody = regEx.replace(source, replacestr)
FwdMsg.HTMLBody = newbody
NewLine = "Dear users,Please note that data is also affected by the incident below and will be corrected. Please email for more information."
FwdMsg.HTMLBody = NewLine & FwdMsg.HTMLBody
FwdMsg.Recipients.Add "xx.com.sg"
FwdMsg.Subject = "Incident" & Format$(Now, " dd-mm-yyyy hh.mmam/pm")
不知何故,当我在Outlook上编写脚本时,我注意到了一些事情。
replacestr
行,但不会在影响和更正操作之间替换这些句子。有什么想法吗?
答案 0 :(得分:1)
怎么样
(Impact:\n)[\s\w]*(\nCorrective\s*Action)
将脚本更改为
replacestr = "$1\n\nplease call me with this number\n\n$2"
strPattern = "(Impact:\n)[\s\w]*(\nCorrective\s*Action)"
将产生输出
Impact:
please call me with this number
Corrective Actio
请参阅正则表达式http://regex101.com/r/gU3aS1/2
上的示例答案 1 :(得分:1)
在我看来,您在使用之前并没有初始化strPattern
:
With regEx
.Global = False
.multiline = True
.ignorecase = False
.pattern = strPattern ' empty string ""
End With
此时,没有为strPattern
分配任何内容,因此它只包含空字符串""
。所以你的正则表达式实际上是在寻找""
的第一次出现,我认为它在你的电子邮件开头就找到了。这显然不是你想要的。
要解决此问题,请将您指定值的行移至strPattern
,使其显示在您使用该变量的位置之前,例如
strPattern = "^(Impact:)\s*(?:(?!^(?:Impact:|Correction[^\S\n]+Action))[\s\S])*^(Correction[^\S\n]+Action)"
With regEx
.Global = False
.multiline = True
.ignorecase = False
.pattern = strPattern ' now it contains what you're looking for.
End With
或者,完全摆脱那个无用的临时变量!我没有看到你在其他任何地方使用它,所以为什么不只是inline呢。
With regEx
.Global = False
.multiline = True
.ignorecase = False
.pattern = "^(Impact:)\s*(?:(?!^(?:Impact:|Correction[^\S\n]+Action))[\s\S])*^(Correction[^\S\n]+Action)"
End With