To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Go Back   ASP Message Board > ASP Technology > Advanced ASP

Advanced ASP This forum is for advanced ASP questions. If you have not received a response from the General ASP Q&A forum, try posting your question here.

Reply
 
Thread Tools Display Modes
  #1  
Old 01-21-2010, 12:39 PM
tpaulengineer tpaulengineer is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Question Classic ASP - RegEx Replace help

Hi All,

I am looking for a little help with using asp's regex replace.
I need to replace all ampersands "&" to "amp;", in a multiline string, but only when these ampersands are found in between the quotes of an href.
The following code works for replacing all the ampersands, but it also replaces them on the img src, which is not what I want.
I am not sure if it is my RegEx or my replace function that is my problem, but if anyone can look at my example below, maybe you can guide me on what to change.


<%
InitialString = "href=""http://video.google.com/videosearch?q=anon+searching&um=1&ie=UTF-8&sa=N&hl=en&ta=wv"">" _&
InitialString & vbcrlf & "img src=""http://images.google.com/image?q=anon+searching&um=1&ie=UTF-8&sa=N&hl=en&ta=wv"">"
%>
<% strOriginalString = ereg_replace(InitialString, "&","amp;", True) %>

<%




function ereg_replace(strOriginalString, strPattern, strReplacement, varIgnoreCase)
' Function replaces pattern with replacement
' varIgnoreCase must be TRUE (match is case insensitive) or FALSE (match is case sensitive)
dim objRegExp : set objRegExp = new RegExp
with objRegExp
.Pattern = strPattern
.IgnoreCase = varIgnoreCase
.Global = True
.Multiline = True
end with
ereg_replace = objRegExp.replace(strOriginalString, strReplacement)
set objRegExp = nothing
end function

response.Write(strOriginalString)
%>
Reply With Quote
  #2  
Old 01-21-2010, 12:55 PM
tpaulengineer tpaulengineer is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Default

I made a typo.. please remove the " _&" if you want to save it to an asp and run it yourself.
Reply With Quote
  #3  
Old 01-21-2010, 07:41 PM
Bill Wilkinson Bill Wilkinson is offline
Senior Member
 
Join Date: Dec 1969
Posts: 93,281
Default

I think you are making a mistake in your methodogy.

Why not replace them before you put them in???

Code:
<% 
InitialString = "href=""" _
    & Replace("http://video.google.com/videosearch?q=anon+searching&um=1&ie=UTF-8&sa=N&hl=en&ta=wv","&","&amp;")
    & """>"
And so on.

I don't believe you can do this with a single simple RegEx replacement.
Reply With Quote
  #4  
Old 01-21-2010, 09:06 PM
tpaulengineer tpaulengineer is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Default

Bill,

Thanks for responding.
You may be right, I may have to look for a different method of attacking this effort.
However, I can't just use the replace function because I will have a large HTML full of hrefs, and everything else. It is not a string I control. It is a mutliline string (HTML) that will be different everytime and I need to parse through and find each href and replace the ampersands.
I am toying with the though of trying to find the position of each "href" and the following double quote and then try to do a replace by the position, but I don't think there is a way.

I am still thinking this should be doable with the RegEx replace. I need to find all X characters, when they appear between A and B substring, and replace the X with Y.

Please feel free to shoot any ideas. Thanks
Reply With Quote
  #5  
Old 01-22-2010, 06:55 PM
Bill Wilkinson Bill Wilkinson is offline
Senior Member
 
Join Date: Dec 1969
Posts: 93,281
Default

You could use the regex to find a list of matches and then do the replace inside the match.

I know how to (for example) replace a single & inside of a pair of "...", but I don't know a way to replace *all* & without using the replace-in-each-match, as above.
Reply With Quote
  #6  
Old 01-24-2010, 11:46 AM
tpaulengineer tpaulengineer is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Default

Thanks Bill. I'll give it a try this week.
Reply With Quote
  #7  
Old 01-24-2010, 11:51 AM
tpaulengineer tpaulengineer is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Default

That can be very challenging because I think I have to grab the 1$ or 2$ right?
Reply With Quote
  #8  
Old 01-28-2010, 11:07 PM
tpaulengineer tpaulengineer is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Default

Ok.. so I figure it out- I found a close enough example online. The majority of the work was done by the online contributor, not me.

Basically it is a mix of regex matching, with Position evaluation and Replace.
In my example I replace "&" with "amp;" but only in the hrefs, not the img src calls.

Here is my code example, I hope it helps others.

<%
'this is the multiline string I am using to test
InitialString = "href=""http://video.google.com/videosearch?q=anon+searching&um=1&ie=UTF-8&sa=N&hl=en&ta=wv"">"
InitialString= InitialString & VBCRLF & "img src=""http://images.google.com/image?q=anon+searching&im=1&ie=UTF-8&sa=N&hl=en&ta=wv"">"
InitialString= InitialString & VBCRLF & "href=""http://www.google.com/image?q=anon+searching&um=2&ie=UTF-8&sa=N&hl=en&ta=wv"">"
response.write(RegExpReplace(InitialString, "href=""[^""]*", "amp;"))


Function RegExpReplace(strInput, strPattern, strReplace)
Dim regEx, Match, Matches, Position, strReturn
Position = 1
strReturn = ""

Set regEx = New RegExp
regEx.Pattern = strPattern
regEx.IgnoreCase = True
regEx.Global = True
regEx.Multiline = True



Set Matches = regEx.Execute(strInput)
For Each Match in Matches
' Below line adds everthing from the current position
' to the next pattern match
strReturn = strReturn & Mid(strInput, Position, Match.FirstIndex+1-Position)

' Then this line replaces the ampersand in the matched value from the regex match
' and appends it to the result.
strReturn = strReturn & Replace(Match.Value, "&", strReplace)
Position = Len(Match.Value) + Match.FirstIndex + 1
Next

' Add any text after the last match
strReturn = strReturn & Mid(strInput, Position, Len(strInput))

RegExpReplace = strReturn
End Function

%>
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 12:16 AM.

More ASP Resources

Reference:
-- ASPFAQs.com
-- VBScript Reference
-- JScript Reference
-- SQL Books Online
-- Official Docs
-- Commonly Asked Messageboard Questions

Resources:
-- Recent ASP Articles
-- ASP.NET Information
-- 4Guys ASP F.A.Q.
-- ASPFAQs.com
-- ASP Internet Resource
-- ASP.NET Internet Resource
-- ASP Coding Tips
-- Newsletter
-- Related Web Technologies
-- User Tips!!
-- ASP-related ListServs

Information:
-- Advertise
-- Author an Article
-- Feedback
-- ASP Messageboard F.A.Q.

Windows Technology
Check out these Web sites for articles, tutorials, FAQs, and code on ASP and related technologies!
-- 15Seconds.com
-- ASP101.com
-- ASPFAQs.com
-- ASPMessageboard.com
-- ASPWire.com

[Complete List of Sites]




Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.