Gossamer Forum
Home : General : Perl Programming :

perl regex help

Quote Reply
perl regex help
hi

can someone help me out with a perl regex...

what I want to do is pull the entire line:

Code:
<input type="hidden" name="url" value="http://www.mysite.com/">

out of the following block of html code.

Code:
<table width="750" border="0" cellspacing="0" cellpadding="0" vspace="0" hspace="0" height="53">
<tr valign="top" align="left">
<td height="34"><input type="hidden" name="url" value="http://www.mysite.com/">
<img src="/header/logo.gif" width="218" height="34" border="0" alt="mysite.com" vspace="0" hspace="0"></a></td>
<td valign="bottom" align="right" nowrap>
<table border="0" cellspacing="0" cellpadding="0" height="36" background="/header/pix.gif">
<tr>

by using the url as the target to match against?

Code:
$htmlblock =~/http://www.mysite.com/g

I can find the url ok using the above, but how do I "grab" the html from the leading "<" to the trailing ">" around it?

thanks

r
Quote Reply
Re: [ryel01] perl regex help In reply to
Hi. You could try something like;

Code:
my $url;
$htmlblock =~ m|\<input type=\"hidden\" name=\"url\" value=\"(.*?)\"\>|i and $url = $1;

print "URL = $url";

Its dirty, but should do what you want.

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [ryel01] perl regex help In reply to
Hey there,

This will do what you want:
Code:
$test = ($htmlblock =~ m|(<input[^<]*http://www.mysite.com[^>]*>)|gis) ? $1 : "Not Found...";

Of course, this is a bit broad since it will capture any type of input field (text, hidden, etc...) which contains the specified URL.

If there is more than one possible match, you can capture them all in an array:
Code:
@test = ($htmlblock =~ m|(<input[^<]*http://www.mysite.com[^>]*>)|gis);
If you really want to look just for hidden fields, use this:
Code:
$test = ($htmlblock =~ m|(<input[^<]*type="hidden"[^<]*http://www.mysite.com[^>]*>)|gis) ? $1 : "Not Found...";

Enjoy! Hope that helps.