Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: My regex works, but I want to make sure it's not blind luck

by eyepopslikeamosquito (Archbishop)
on Dec 22, 2020 at 22:03 UTC ( [id://11125645]=note: print w/replies, xml ) Need Help??


in reply to My regex works, but I want to make sure it's not blind luck

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:

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125645]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found