Discussion:
RegExp without duplicate matches
(too old to reply)
ljb
2006-04-07 15:45:09 UTC
Permalink
I using the following to create hyperlinks of every 7 digit number in an
html table. The problem is there are some duplicate numbers and they create
href's within href's. How do I get rid of the duplicates in a matches
collection before I use vbscript's replace function? I first thought of
using regular expression's replace method and sub-matches but wasn't
successful. The desired result is given 1234567 create
href="./7/1234567.shtml"

sub MakeHyperlinks(strTable)
dim match, regEx

set regEx = Server.CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.Pattern = "\d{7}"

for each match in regEx.Execute(strTable)
strTable = Replace( _
strTable, _
match, _
"<a href=""./" & right(match,1) & "/" & match & ".shtml"">" & match &
"</a>")
next

set regEx = nothing
end sub

thanks
LJB
ekkehard.horner
2006-04-07 18:04:15 UTC
Permalink
Post by ljb
I using the following to create hyperlinks of every 7 digit number in an
html table. The problem is there are some duplicate numbers and they create
href's within href's. How do I get rid of the duplicates in a matches
collection before I use vbscript's replace function? I first thought of
using regular expression's replace method and sub-matches but wasn't
successful. The desired result is given 1234567 create
href="./7/1234567.shtml"
sub MakeHyperlinks(strTable)
dim match, regEx
set regEx = Server.CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.Pattern = "\d{7}"
for each match in regEx.Execute(strTable)
strTable = Replace( _
strTable, _
match, _
"<a href=""./" & right(match,1) & "/" & match & ".shtml"">" & match &
"</a>")
next
set regEx = nothing
end sub
thanks
LJB
Use RegExp.Replace - e.g.:

Dim sSrc : sSrc = "aaa12aa34aaa12aaa34aaa56"
Dim sRpl : sRpl = "href=$1.htm"
Dim sExp : sExp =
"aaahref=12.htmaahref=34.htmaaahref=12.htmaaahref=34.htmaaahref=56.htm"

Dim oRE : Set oRE = New RegExp
oRE.Pattern = "(\d{2})"
oRE.Global = True

Dim sRes
sRes = oRE.Replace( sSrc, sRpl )
WScript.Echo CStr( sRes = sExp ), sRes

Loading...