查找文本并替换为超链接
我正在尝试将正文中的文本替换为模式 ASA####@@ 到 ASA####@@(超链接)
我有代码,如果正文中只有一种模式,则该代码可以工作。
但如果我有很多像
ASA3422df 这样的模式
ASA2389ds
ASA1265sa
整个机身被替换为
ASAhuyi65
我的代码在这里。
Dim strID As String
Dim Body As String
Dim objMail As Outlook.MailItem
Dim temp As String
Dim RegExpReplace As String
Dim RegX As Object
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
Body = objMail.HTMLBody
Body = Body + "Test"
objMail.HTMLBody = Body
Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Pattern = "ASA[0-9][0-9][0-9][0-9][a-z][a-z]"
.Global = True
.IgnoreCase = Not MatchCase
End With
'RegExpReplace = RegX.Replace(Body, "http://www.code.com/" + RegX.Pattern + "/ABCD")
'if the replacement is longer than the search string, future .FirstIndexes will be off
Offset = 0
'Set matches = RegX.Execute(Body)
For Each m In RegX.Execute(Body)
RegExReplace = "<a href=""http://www.code.com/" & m.Value & """>" & m.Value & "</a>"
Next
Set RegX = Nothing
objMail.HTMLBody = RegExReplace
objMail.Save
Set objMail = Nothing
End Sub
I am trying to replace text in the body with pattern ASA####@@ to ASA####@@(hyperlink)
I have code which works if there is only one pattern in the body.
But if I have many patterns like
ASA3422df
ASA2389ds
ASA1265sa
the entire body gets replaced to
ASAhuyi65
My code is here.
Dim strID As String
Dim Body As String
Dim objMail As Outlook.MailItem
Dim temp As String
Dim RegExpReplace As String
Dim RegX As Object
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
Body = objMail.HTMLBody
Body = Body + "Test"
objMail.HTMLBody = Body
Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Pattern = "ASA[0-9][0-9][0-9][0-9][a-z][a-z]"
.Global = True
.IgnoreCase = Not MatchCase
End With
'RegExpReplace = RegX.Replace(Body, "http://www.code.com/" + RegX.Pattern + "/ABCD")
'if the replacement is longer than the search string, future .FirstIndexes will be off
Offset = 0
'Set matches = RegX.Execute(Body)
For Each m In RegX.Execute(Body)
RegExReplace = "<a href=""http://www.code.com/" & m.Value & """>" & m.Value & "</a>"
Next
Set RegX = Nothing
objMail.HTMLBody = RegExReplace
objMail.Save
Set objMail = Nothing
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您最初使用注释掉的行是在正确的轨道上。使用
Replace
方法不需要循环匹配(这就是Global
标志的用途),并且可以使用像$1
这样的反向引用,$2
等作为匹配子字符串的占位符。与大多数语言一样,Regular-Expressions.info 上有针对 VBScript 的专用页面。以下内容将执行您要查找的操作:
这将用链接替换匹配项(并且仅匹配项),并保持其他所有内容不变。
It looks like you were on the right track originally with that commented-out line. With the
Replace
method don't need to loop over matches (that's what theGlobal
flag is for), and can use backreferences like$1
,$2
, etc. as placeholders for matching substrings. As with most languages, there's a dedicated page on Regular-Expressions.info for VBScript.The following with do what you're looking for:
This replaces the matches (and only the matches) with a link, and leaves everything else untouched.
在 codedawn,有一个很棒的 Excel 插件,它为您提供与您熟悉和喜爱的相同的 UI 搜索和替换,但适用于正则表达式。
http://www.codedawn.com/excel-add-ins.php
虽然这并不能完全帮助回答您的问题,但对于在不更改数据或代码的情况下逐个尝试正则表达式非常有用。
Over at codedawn, there is a fantastic add-in for Excel that gives you the same UI search and replace that you know and love, but for regular expressions.
http://www.codedawn.com/excel-add-ins.php
While this doesn't exactly help answer your question, it's useful for trying out regular expressions one after another without altering data or code.