Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

I want to creat a reg exp for it

Posted on 2005-02-28
9
Medium Priority
?
228 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

580 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