80degreez has asked for the wisdom of the Perl Monks concerning the following question:

I wrote this small script that uploads a picture to TinyPic.com using WWW::Mechanize, but I've never been that good with pattern matching and I'm trying to extract the URL to the image after its been uploaded from the pages contents. Script Code
use strict; use WWW::Mechanize; use HTTP::Cookies; my $mech = WWW::Mechanize->new( stack_depth => 1, agent =>"Mozilla/5.0 + (X11; U; Linux i68+6; en-US; rv:1.7) Gecko/20040918 Firefox/0.9.3 ") +; my $pic = "C:\/LV3.JPG"; $mech->cookie_jar( HTTP::Cookies->new() ); $mech->get("http://tinypic.com"); if($mech->content() =~ /UPLOAD NOW/ ){ print "TinyPic loaded... "; $mech->form_name("upload_form"); $mech->field('the_file' => $pic); $mech->submit(); }
and this is the mech->content from the page after it's been uploaded
<html> <head> <meta http-equiv="Content-Type" content="text/html; char +set=iso-8859-1" /> <meta name="description" content="TinyPic, a Photobucket + Video and Image Hosting company" /> <meta name="keywords" content="images, photos, videos, m +yspace, ebay, video hosting, photo hosting" /> <title>TinyPic - Share The Experience!&trade;</title> <link href="http://static.tinypic.com/s/stylesheet.css" +rel="stylesheet" type="text/css" /> </head> <body> <table width="100%" height="95%"> <tr> <td valign="middle" align="center"> <form name="myform" method="post" action="http://tiny +pic.com/index.php" target="_parent"> <input type="hidden" name="pic" value="86gjua9" /> <input type="hidden" name="s" value="1"> <input type="hidden" name="ival" value="11" /> <input type="hidden" name="type" value=".jpg" /> <input type="hidden" name="domain_lang" value="en" /> <script language="javascript">document.myform.submit( +)</script> </form> <strong><a href="http://tinypic.com/view.php?pic=86gj +ua9&s=1" target="_blank">Click here</a> to view your image</strong> </td> </tr> </table> </body> </html>
http://tinypic.com/view.php?pic=86gjua9 is what I'm trying to get it to pull from the page content

Replies are listed 'Best First'.
Re: Pattern Matching Q?
by educated_foo (Vicar) on Nov 27, 2007 at 18:32 UTC
    So you want to match:
    m{ <strong # match "<strong" .*? # skip to the next ( # (start capturing) http:// # "http://" .*? # skip to the next ) # (stop capturing) & # ampersand }x
    Or, more concisely, m{<strong.*?(http://.*?)&}.
      With that code that you pasted to me, where would the resulting match be stored?
        All pattern matches are stored in $1, $2, $3 depending on the set of parens that matched. In this case it will be stored in $1.

        Another way of looking at this is (to borrow from educated_foo's answer: ($url = $1) if (m{<strong.*?(http://.*?)&});