查找文本并替换为超链接 [英] Find text and replace with hyperlink

查看:121
本文介绍了查找文本并替换为超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将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
 

解决方案

看起来您原来在正确的轨道上并带有注释掉的行.使用Replace方法不需要循环匹配(这是Global标志的作用),并且可以使用诸如$1$2等的反向引用作为匹配子字符串的占位符.与大多数语言一样,关于VBScript的Regular-Expressions.info上的专用页面. /p>

以下内容可满足您的需求:

body = "Blah blah ASA3422df ASA2389ds ASA1265sa"
body = RegX.Replace(body, "<a href='http://www.code.com/$1'>$1</a>")
Debug.Print body 
'-> Blah blah <a href='http://www.code.com/ASA3422df'>ASA3422df</a> <a href='http://www.code.com/ASA2389ds'>ASA2389ds</a> <a href='http://www.code.com/ASA1265sa'>ASA1265sa</a>

这将匹配项(仅匹配项 )替换为链接,并保持所有其他内容不变.

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

解决方案

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 the Global 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:

body = "Blah blah ASA3422df ASA2389ds ASA1265sa"
body = RegX.Replace(body, "<a href='http://www.code.com/$1'>$1</a>")
Debug.Print body 
'-> Blah blah <a href='http://www.code.com/ASA3422df'>ASA3422df</a> <a href='http://www.code.com/ASA2389ds'>ASA2389ds</a> <a href='http://www.code.com/ASA1265sa'>ASA1265sa</a>

This replaces the matches (and only the matches) with a link, and leaves everything else untouched.

这篇关于查找文本并替换为超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆