【问题标题】:How to delete certain sentences between 2 words before forwarding the email?如何在转发电子邮件之前删除两个单词之间的某些句子?
【发布时间】:2014-10-25 17:02:53
【问题描述】:

我已经成功编写了一个可以更改主题然后转发给新收件人的脚本。

我目前的剧本是这样的

With FwdMsg
NewLine = "Dear users,Please note that we are affected by the incident below and will be corrected after systems recovers. Please email the team  for more information.Thank you."
FwdMsg.HTMLBody = NewLine & FwdMsg.HTMLBody 

FwdMsg.Recipients.Add "hidden@mail"
FwdMsg.Subject = "Incident affecting you" & Format$(Now, " dd-mm-yyyy hh.mmam/pm")
FwdMsg.send

但是我现在需要删除电子邮件中的某些句子并将其替换为please call me.

这是我的电子邮件正文:

Impact:
ABCD EFG RSTUV
ASDFT
Corrective Action

我需要删除 AABCD EFG RSTUV ASDFT 并替换为句子 please call me 以便新的电子邮件正文为:

Impact:

please call me with this number

Corrective Action

如何用正则表达式做到这一点?

我试过了,但它似乎不起作用

  Dim pattern As String
  pattern = "Impact:.*Correction"
  Msginput " " & pattern
  Dim Msginput As String
  Msginput = FwdMsg.HTMLBody
  MsgBox " " & Msginput
   Dim replacement As String
   replacement = "please call me"
   Dim rgx As New RegExp
  Dim result As String
  result = rgx.Replace(Msginput, replacement)
  FwdMsg.HTMLBody = result

【问题讨论】:

  • 你的正则表达式应该是:(?<=Impact\:).*(?=Corrective Action)
  • 还需要指定RegexOptions.Multiline

标签: regex vb.net email


【解决方案1】:

您可以使用查找和替换捕获缓冲区来控制输出格式。
必须使用正则表达式 MULTILINE 选项,因为行首用作分隔符。

查找:

^(Impact:)\s*(?:(?!^(?:Imapct:|Corrective[^\S\n]+Action))[\s\S])*^(Corrective[^\S\n]+Action)

替换:

$1\n\nplease call me with this number\n\n$2

扩展正则表达式:

 ^ 
 ( Impact: )
 \s* 
 (?:
      (?!
           ^ 
           (?:
                Imapct:  
             |  Corrective [^\S\n]+ Action
           )
      )
      [\s\S] 
 )*
 ^ 
 ( Corrective [^\S\n]+ Action )

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 2013-10-28
    • 2012-04-17
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多