Gossamer Forum
Home : General : Perl Programming :

perl regex help

Quote Reply
perl regex help

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

what I want to do is pull the entire line:

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

out of the following block of html 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">

by using the url as the target to match against?

$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?


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

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.


Andy (mod)
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:
$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:
@test = ($htmlblock =~ m|(<input[^<]*http://www.mysite.com[^>]*>)|gis);
If you really want to look just for hidden fields, use this:
$test = ($htmlblock =~ m|(<input[^<]*type="hidden"[^<]*http://www.mysite.com[^>]*>)|gis) ? $1 : "Not Found...";

Enjoy! Hope that helps.