Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Regex to extract last 3 components of filename

by hippo (Bishop)
on May 04, 2021 at 21:51 UTC ( [id://11132045]=note: print w/replies, xml ) Need Help??


in reply to Regex to extract last 3 components of filename

Note that if I were doing this for real I would not use a regex match to accomplish it but would use split for brevity or a module to cover edge cases and portability. However, here is one solution. This might not achieve what you want because it isn't clear precisely what you do want. It should provide a starting point at least.

use strict; use warnings; use Test::More; my @tests = ( { in => ' /x/y/z/a/b/c/d/e.f', want => '/c/d/e.f' } ); plan tests => scalar @tests; my $re = qr#((?:/[^/]*?){3})$#; for my $t (@tests) { $t->{in} =~ $re; is $1, $t->{want}, "Extracted $t->{want}"; }

🦛

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found