I pulled a face the instant I caught sight of your ^.*(\..*)$ regex: the leading ^.* looks pretty pointless, the trailing (\..*) overly generic. For some background on where I'm coming from see the classic old node from 2000: Death to Dot Star! by Ovid.

Given you say "the image is guaranteed to end with the picture extension" I would write it something like: /\.([^.]+)$/ or /\.([a-zA-Z0-9]+)$/ or /\.(\w+)$/ or some such, depending on your requirements, the point being to be more precise than the dreaded "dot star". To illustrate, using GrandFather's example test program:

use strict; use warnings; my @tests = ( ".", "", "A sentence.", ".gitignore", "word.doc", "a.dotted.name", ); print /\.([^.]+)$/ ? "Matched '$1' in " : "Failed", " '$_'\n" for @tes +ts;
produces:
Failed '.' Failed '' Failed 'A sentence.' Matched 'gitignore' in '.gitignore' Matched 'doc' in 'word.doc' Matched 'name' in 'a.dotted.name'

If you give us a lot more specific examples of strings that should match and ones that shouldn't, we can offer a more precise regex.

See also: Rosetta code

Update: For an alternative to regexes, using instead mostly standard Perl facilities, such as glob, opendir, readdir, File::Glob, File::Basename, File::Copy, File::Spec, Path::Tiny and Path::Class::Dir, see:


In reply to Re: My regex works, but I want to make sure it's not blind luck by eyepopslikeamosquito
in thread My regex works, but I want to make sure it's not blind luck by SergioQ

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.