Assuming the image name doesn't have any spaces in it, you could do it like this:

if ( $url =~ m!/([^/]+\.(?:jpe?g|gif|png|tiff?)\b)!i ) { print "Found $1\n"; }

The way this works is as follows:

m! # Use an alternate delimiter. / # Match a '/' character to anchor off of. ( # Start a capturing parenth. [^/]+ # Match any number of non-'/' characters. \. # Match the '.' character. (?: # Group or constrain without capturing. jpe?g # jpg or jpeg. | gif # or gif | png # or png | tiff? # or tif or tiff ) # End the grouping/constraining parens. \b # Make sure that tiff isn't tiffany, for # example. ) # End the capture. !ix # Case insensitive /i (Ignore the x, it's only # necessary in this expanded example.

If the URL is encoded, so that spaces have become '+' characters, etc., you will need to decode the URL first, or else filenames containing spaces will be mangled. That particular issue isn't a regexp issue, just the way URL's get encoded.


Dave


In reply to Re: Regex simple quicky question :) by davido
in thread Regex simple quicky question :) by tanger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.