JanFi86
JanFi86

Reputation: 519

Get original filename from zipped file

I have zipped files names like:

filename1.csv.Z
filename2.csv.gz

I have regex expression for getting the last extension:

my ($ext) = $file =~ /(\.[^.]+)$/;

Now I would like to get the "original filename", without .Z or .gz, eg. filename1.csv, filename2.csv

I tried to use something like expresion below but does not work...

my ($output) = $file =~ /.+?(?=(([^.]+)$))/;

Would anybody help me out?

Upvotes: 1

Views: 113

Answers (2)

The fourth bird
The fourth bird

Reputation: 163277

In your pattern .+?(?=[^.]+$) you assert what is directly on the right is 1+ times not a dot followed by the end of the string.

That assertion could be true at multiple places and will also include matching the dot itself.

For example in filename2.csv.gz that will match filename2.csv. and g

If you want to get the match only of the filename in the example data, you could add the dot in the positive lookahead:

.+(?=\.[^.]+$)

Regex demo

Or use a capturing group and a match instead of a lookahead:

(.+)\.[^.]+$

Regex demo | Perl demo

For example

my $line = "filename1.csv.Z";
my ($output) = $line =~ m/(.+)\.[^.]+$/;
print $output;

# filename1.csv

Upvotes: 0

daxim
daxim

Reputation: 39158

Know your standard library. See File::Basename:

use File::Basename qw(basename);
for my $fullname (qw(filename1.csv.Z filename2.csv.gz)) {
    my $basename = basename $fullname, qw(.Z .gz);
}
__END__
filename1.csv
filename2.csv

Upvotes: 6

Related Questions