Problems? Is your data what you think it is? | |
PerlMonks |
Re: My regex works, but I want to make sure it's not blind luckby eyepopslikeamosquito (Archbishop) |
on Dec 22, 2020 at 22:03 UTC ( [id://11125645]=note: print w/replies, xml ) | Need Help?? |
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: produces:
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 Section
Seekers of Perl Wisdom
|
|