[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

regular expression in VBScript

Posted on 2002-03-04
16
Medium Priority
?
378 Views
Last Modified: 2008-02-01
Hi,

I have a string with some links in there
for example: str1 = " this is link1, link2 and link3"

I can use regular expression to find the links then make the Matches Object. Then use
  For Each strMatch In strMatches
    'with strMatch.Value and strMatch.Index
    'to replace link1 to alink1, link2 to alink2,
    'and link3 to alink3.
  Next
The problem is if link1=link2 then at the end I will have aalink1, aalink2, and alink3.  (notice double a's)
That is because, the first replace replaces link1 to alink1, then the second replace (for link2) will replace link1 in alink1 to alink1 -> then result is aalink1.

Does anyone know how to fix that problem?

Thanks in advance.


0
Comment
Question by:phuctran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +3
16 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 6840051
a quick fix would be to run another expression after the first one that replaces "aalink" with "alink"
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6840054
or, in the first expression, instead of scanning for "link" , scan for " link"  ( \slink )
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6840188
Don't do this with For Each, loop through the string one time only.  This might be different from what you are looking for, but you get the idea;

<%
sString = "this is link1, link2 and link3"
Response.Write "Search string:" &sString

Set RegX = NEW RegExp
SearchPattern = "link"
RegX.Pattern = SearchPattern
RegX.Global = True
ReplacedText = RegX.Replace(sString, "a"&RegX.Pattern)

Response.Write "<p>New String: "&ReplacedText
%>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Author Comment

by:phuctran
ID: 6840748
Sorry, I am not clear on my question.

The word is not "link".

for example:

this is www.yahoo.com, www.excite.com, and www.yahoo.com

I want to create http links for that string, so when I display it on browsers, I can click on that to go to the site.
0
 
LVL 3

Author Comment

by:phuctran
ID: 6840755
just like this page, after I type www.yahoo.com then submit, there is a link for that.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6840901
mgfranz,

<off topic>
http://devx.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20192557
</off topic>

Anthony
0
 
LVL 3

Expert Comment

by:pbryan
ID: 6841109
try this function
----------------------
<%
Function replacelinks(textin)
Dim intLinkStartPos
Dim intLinkEndPos
Dim textout
Dim intLinkLenInc
Dim strLink
intLinkStartPos = InStr(textin, "http:")
If intLinkStartPos = 0 Then intLinkStartPos = InStr(textin, "https:")
If intLinkStartPos = 0 Then
    intLinkStartPos = InStr(textin, "www.")
Else
    If InStr(textin, "www.") <> 0 Then
        If intLinkStartPos>InStr(textin, "www.") Then
            intLinkStartPos = InStr(textin, "www.")
        End If
    End If
End If
While intLinkStartPos > 0
   
    intLinkEndPos = InStr(intLinkStartPos, textin, " ")
    If intLinkEndPos < intLinkStartPos Then
        intLinkEndPos = Len(textin) + 1
    End If
    strLink = Mid(textin, intLinkStartPos, intLinkEndPos - intLinkStartPos)
    If InStr(strLink, ",") > 0 Then
        strLink = Left(strLink, InStr(strLink, ",") - 1) & Mid(strLink, InStr(strLink, ",") + 1)
    End If
    intLinkLenInc = 16 + Len(strLink)
    If Left(Mid(textin, intLinkStartPos, intLinkEndPos - intLinkStartPos), 4) = "http" Then
        textin = Left(textin, intLinkStartPos - 1) & " <a href=" & Chr(34) & strLink & Chr(34) & ">" & Mid(textin, intLinkStartPos, intLinkEndPos - intLinkStartPos) & "</a>" & Mid(textin, intLinkEndPos )
    Else
        intLinkLenInc = intLinkLenInc + 7
        textin = Left(textin, intLinkStartPos - 1) & " <a href=" & Chr(34) & "http://" & strLink & Chr(34) & "> " & Mid(textin, intLinkStartPos, intLinkEndPos - intLinkStartPos) & "</a>" & Mid(textin, intLinkEndPos )
    End If
     
         
    intLinkStartPos = InStr(intLinkEndPos + intLinkLenInc, textin, "http:")
    If intLinkStartPos = 0 Then intLinkStartPos = InStr(intLinkEndPos + intLinkLenInc,textin, "https:")
    If intLinkStartPos = 0 Then
        intLinkStartPos = InStr(intLinkEndPos + intLinkLenInc, textin, "www.")
    Else
        If InStr(intLinkEndPos + intLinkLenInc, textin, "www.") <> 0 Then
            If intLinkStartPos > InStr(intLinkEndPos + intLinkLenInc, textin, "www.") Then
                intLinkStartPos = InStr(intLinkEndPos + intLinkLenInc, textin, "www.")
            End If
        End If
    End If
Wend
replacelinks = textin
end function
%>
0
 
LVL 3

Expert Comment

by:pbryan
ID: 6841113
you use the function like this

<%=replacelinks("this is www.yahoo.com, www.excite.com,  and www.yahoo.com")%>

or

<%=replacelinks("this is http://www.yahoo.com, www.excite.com, and https://www.mysecuresite.com")%>

0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6841160
Don't be daft, a Regular expression is the only way to do this, and this is why:

http://aspemporium.com/aspEmporium/src/vbs_srcview.asp?source=regexp_LinkURLs.asp

You'll see how easy it is!!! ;-)
0
 
LVL 18

Accepted Solution

by:
mgfranz earned 800 total points
ID: 6841678
Wow, thats a lot of code pbryan.

A simple RegExp is all you need, the following takes a parsed input, check to see if the letters "www" or anything following ://, and creates a "link" out of it;

sString = Server.HTMLEncode(rs.Fields("Notes"))

Set RegX = NEW RegExp
SearchPattern = "(((\w+):\/\/|www)([^\s]+\w))" RegX.Pattern = SearchPattern
RegX.Global = True
a = "$1"
b = "$2"
c = "http://$1
ReplaceString = "<a href='"&Trim(c)&"'>"&Trim(a)&"</a>"

ReplacedText = RegX.Replace(sString, ReplaceString)
ReplacedText = Replace(ReplacedText, "http://http://", "http://", VBTextCompare)

Response.Write ReplacedText
%>

Wrap it in a Function.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6841680
You can delete the "b" match.
0
 
LVL 3

Author Comment

by:phuctran
ID: 6842159
AlfaNoMore,

Please find a 200-points question "points for AlfaNoMore" and accept that so I can give you the credit.

Thank you.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6842823
Diid you not mean to accept my comment as an answer?  If not, just send a message to Community Support to ask for a change.
0
 
LVL 3

Author Comment

by:phuctran
ID: 6843079
mgfranz,

I accept yours and AlfaNoMore's.

However, the pattern is not used for all cases.  For example, if you have

s = "this is http://www.yahoo.com and <img src='http://www.yahoo.com/images/thisimage.gif'>"  then the http://... in src="" will be changed to <a href=""> also.

The link AlfaNoMore gave me have the same problem.

I will work on that case.  You gave me the right direction (using $1, $2); therefore, the points are yours.

Thank you.

0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6843093
Ahh, I see... A simple edit to the RegExp would solve this.  I'm sure you can kludge it up.
0
 
LVL 3

Author Comment

by:phuctran
ID: 6843117
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question