Gossamer Forum
Home : General : Perl Programming :

Regular expression question

Quote Reply
Regular expression question
Hi guys,

I tried ti use the reg-exp <(.+?)> to parse an image. It prints img src=blah.gif width=... height=... border=...
I need to print the opening and the closing tags. I mean this ">" and this ">".
How can I do?
Thanks for your help.
Gianmarco

Quote Reply
Re: Regular expression question In reply to
Sorry I meant I'd liek to grab this sign "<" and this ">" too.

Quote Reply
Re: Regular expression question In reply to
use this instead..

(<img.+?>)

this will guarantee you an img tag...

Jerry Su
widgetz sucks
Quote Reply
Re: Regular expression question In reply to
also.. the ( and ) tell where to start and stop what to parse

Jerry Su
widgetz sucks
Quote Reply
Re: Regular expression question In reply to
Hi Jerry,
thanks for your answer.
The regexp you provide to me works, but I would hace to parse a link too.
I mean a linked image. But I have to remove the signs "<" and ">" in the <a href="..."> and to keep them in the <img src="..."> tag.
Any idea?
Many thanks!

Gianmarco

Quote Reply
Re: Regular expression question In reply to
i have no idea what you are talking about

Jerry Su
widgetz sucks
Quote Reply
Re: Regular expression question In reply to
Hi,
sorry for my question, but maybe my english is not so good...:)
Maybe it's better if I try to explain with an example.

The HTML code of what I want to parse is:

<td align=center width=25% ><A href="http://www.mysite.com/page1.html"><img src="pic1.gif" width="100" height="100" alt="Pic" border="0"></A>

What I would need to print is only

http://www.mysite.com/page1.html and the entire tag <img src="pic1.gif" width="100" height="100" alt="Pic" border="0">

Now, I succed in printing only one tag a time but never both together.

I tried to use this regexp:

<td align=center width=25% ><A\W+href=[\"\']?([^>\"\']+)[^>]*>(<img.+?>)</A>

but it doesn't work.
As you surely have noticed I'm new to regular expression... :))

Thanks you again for your help!

Gianmarco

Quote Reply
Re: Regular expression question In reply to
umm.. try this..

while ($src =~ m#<a href="([^"]+?)">(<img.*?>)</a>#ig) {
my ($url, $img) = ($1, $2);
#print
}

Jerry Su
widgetz sucks