Reputation: 23
I had earlier asked a question at this link on regex How to delete certain sentences between 2 words before forwarding the email?
I went back and tried the suggestion and made some observations. The below is my script where I am trying to:
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")
Somehow, I noticed a few things when I wrote my script on my Outlook.
replacestr
line gets displayed on the email but not replacing those sentences between the words Impact and Correction Action.Any ideas?
Upvotes: 2
Views: 107
Reputation: 38551
Looks to me like you didn't initialize strPattern
before using it:
With regEx
.Global = False
.multiline = True
.ignorecase = False
.pattern = strPattern ' empty string ""
End With
At this point, nothing has been assigned to strPattern
, so it contains nothing but an empty string ""
. So your regex is literally looking for the first occurrence of ""
, which I presume it finds right at the start of your e-mail. This is obviously not what you want.
To fix this, move the line in which you assign a value to strPattern
up so it appears before the place where you use that variable, e.g.
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
Or, just get rid of that useless temp variable altogether! I don't see you using it anywhere else, so why not just inline it.
With regEx
.Global = False
.multiline = True
.ignorecase = False
.pattern = "^(Impact:)\s*(?:(?!^(?:Impact:|Correction[^\S\n]+Action))[\s\S])*^(Correction[^\S\n]+Action)"
End With
Upvotes: 1
Reputation: 26687
How about
(Impact:\n)[\s\w]*(\nCorrective\s*Action)
changing the script as
replacestr = "$1\n\nplease call me with this number\n\n$2"
strPattern = "(Impact:\n)[\s\w]*(\nCorrective\s*Action)"
will produce output as
Impact:
please call me with this number
Corrective Actio
see the example on regex http://regex101.com/r/gU3aS1/2
Upvotes: 1