?
Solved

I want to creat a reg exp for it

Posted on 2005-02-28
9
Medium Priority
?
224 Views
Last Modified: 2008-03-10
Hi
Suppose that I have saved the content of an HTML file in a variable, named strContent. I want to find all locations in this HTML matching this pattern:

<img src="......................" ....>
Where src DOES NOT start with "http://", and change them to such:
<img src="http://domain.com/folder/............................" ....>
That is, I want to add http://domain.com/folder/ to the begining of the SRC attributes which have missed it.

How would you design a regexp for it?
Huji
0
Comment
Question by:huji
[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
  • 4
  • 4
9 Comments
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13422890
"/(?:[sS][rR][cC]\s*=)(?:[\s""']*)(.*?)(?:[\s>""'])/im"

i used this in a php page, and it returns ALL the src attributes ... including all the ones with the http:// ... so you may need to change this around ... it also returns javascript src's
0
 
LVL 14

Author Comment

by:huji
ID: 13423163
Well I coded what I needed in PHP, very simply. We should accept that PHP uses RegExp in a better way when compared with ASP (VBscript indeed.)
Also, I've designed a regexp that finds the "src"s in ASP. But the problem is with the replacing part. In PHP you can simply choose the second sub-pattern to be changed, but you can't do the same in ASP.... I've coded some ways, but it is not neat and tidy, AT ALL. That's why I asked it here.



EVERYBODY, please provide me with the full code, that replaces the needed parts in the string.

Thanks
huji
0
 
LVL 8

Expert Comment

by:fozylet
ID: 13427509
this does the find/replace thing well... but does not exclude the ones with http... some tuning needed on regex

<%
Option Explicit

Dim str
str = "<img src=""logo.gif"" >sfgjh<img src=""banner.gif"" >"

Dim objRegExp
Set objRegExp = New RegExp

objRegExp.IgnoreCase = True
objRegExp.Global = True

'Repalce all instances of Perl with ASP
objRegExp.Pattern = "(<img src=)(( *|\"")?)([^http:\/\/])([^\""]*)(( *|\"")?)( *)>"
str = objRegExp.Replace(str, "<img src=""http://gcek.net/img/$5"">")
response.write str

Set objRegExp = Nothing        'Clean up!

%>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
fozylet earned 2000 total points
ID: 13427589
This one is better... http is aught now... :)

<%
Option Explicit

Dim str
str = "<img src=""http://photos1.blogger.com/img/163/3635/320/IMG_2675.jpg"" >sfgjh src=fdg <java src=some.js> <a href=http://gcek.net><img src=""banner.jpg"" ></a>"

Dim objRegExp
Set objRegExp = New RegExp

objRegExp.IgnoreCase = True
objRegExp.Global = True

'Repalce all instances of Perl with ASP
objRegExp.Pattern = "(<img src=)(( *|\"")?)(?!http:)([^\""]*)(( *|\"")?)( *)>"
str = objRegExp.Replace(str, "<img src=""http://gcek.net/img/$4"">")
response.write str

Set objRegExp = Nothing        'Clean up!

%>
0
 
LVL 14

Author Comment

by:huji
ID: 13427694
Well, I still need some RegExp references! I feel there are things for me to learn! the $4 was the key, but I hadn't seen it before! :o(      (Would be glad if you recommend me a good online resource)

Now some questions:
1) About the pattern you made:
"(<img src=)(( *|\"")?)(?!http:)([^\""]*)(( *|\"")?)( *)>"
-----------------^
What is the marked part supposed to do? To match things like :       src= something.jpg         ??? (I'm specially focusing on the SPACE before the astrisk.
2) What if one IMG tag is like this:
<img alt="" src="a.jpg">
This pattern will miss that. I'm going to add a part like [^>]* after the (<img       in your pattern. Can you help me with it?
Thanks a lot
Huji
0
 
LVL 8

Expert Comment

by:fozylet
ID: 13427903
i was looking at this page while doing that...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/scriptinga.asp

well... microsoft's version of regexp is always a bit confusing! :)
0
 
LVL 14

Author Comment

by:huji
ID: 13428180
fozylet,
I fixed it myself. Thank you for sharing your knowledge.
Good luck
Huji
0
 
LVL 8

Expert Comment

by:fozylet
ID: 13428324
Thanks Huji :)
Any time!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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