Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions in perl regular expressions 1

Status
Not open for further replies.

kwckwckwc

Programmer
Joined
Nov 8, 2006
Messages
2
Location
US
I'm wanting to $PAGE =~ s/href="#"(.*)<img name="(.*)" src/href="$2"$1<img name="$2"/si;

which means I want to replace a # with the name of the image used in a link.

Now, what I really want to do is lookup the image name and find out what the real link is. I think that means I need a function to take the image name and find the target link, which exists in another file in my applications.

Is there a way to do this? If not, I will write a program to do it. I'd use a regular expression if it could be done.

Anybody game for this one?

Ken
 
You need to give us a sanity check example. Provide a before and after for at least one link so that we can be sure that the regex is doing what you truly want.

I can point out one problem without much thought whatsoever though:

".*" = BAD.

You need to watch out for greedy matching. Your current regular expression will match a WHOLE lot of text all the ways until the last possible img tag. This is probably not want you want. Instead use ".*?"

Ultimately I would probably suggest that you use HTML::Parser instead of a regular expression for this problem. But lets see an example of a translation and maybe we can suggest a regex solution.
 
Thanks for the .* advice. That would have gotten me into trouble down the road, I'm sure.

I think I'll take your suggestion and go read up on HTML::Parser first.

I really appreciate this help. Thanks, Ken
 
HTML::Parser is quite tricky to use. I'm a big fan of HTML::TokeParser::Simple, which is well worth getting to grips with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top