查找文本并替换为超链接

发布于 2024-11-16 21:24:47 字数 1177 浏览 1 评论 0原文

我正在尝试将正文中的文本替换为模式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2024-11-23 21:24:47

看来您最初使用注释掉的行是在正确的轨道上。使用 Replace 方法不需要循环匹配(这就是 Global 标志的用途),并且可以使用像 $1 这样的反向引用, $2 等作为匹配子字符串的占位符。与大多数语言一样,Regular-Expressions.info 上有针对 VBScript 的专用页面

以下内容将执行您要查找的操作:

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>

这将用链接替换匹配项(并且匹配项),并保持其他所有内容不变。

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.

暮倦 2024-11-23 21:24:47

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文