#!/usr/bin/perl use File::Basename; use strict; my @files = ( 'http://server.com/subdir/index.html', 'http://server.com/subdir/dist.tar.gz', 'http://server.com/whatever.cgi?testing=1', 'ftp://server.com/pub/whatever.zip', 'file://local/subdir/testing.txt', ); foreach my $file ( @files ){ my $suffix = ( fileparse( $file, '\..*$' ) )[2]; $suffix =~ s/(\.?[^.?]*)?\?.*?$/$1/; print $suffix, "\n"; }
Note that this code will not handle multi-level extensions, such as .tar.gz. The extension for dist.tar.gz will be reported as .gz (same deal with demerphq's code).
For extensions of this type, you'll probably need to create an array that's propagated with valid file extensions. Coincidentally, you can throw this array at File::Basename to easily ignore invalid extensions. Example:
The above code will list a suffix only for the file types noted in @valid_extensions (not the txt or cgi files).my @valid_extensions = qw/ .tar.gz .html .zip /; foreach my $file ( @files ){ my $suffix = ( fileparse( $file, @valid_extensions ) )[2]; print $suffix, "\n"; }
Jasmine
In reply to Re: Regex to match file extension in URL
by Jazz
in thread Regex to match file extension in URL
by Amoe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |