Is there a regular expression in Perl to find a file's extension? For example, if I have "test.exe
", how would I get the ".exe
"?
m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.
There are many programs designed for programmers available for download on the web. As a Perl convention, a Perl file must be saved with a . pl or.PL file extension in order to be recognized as a functioning Perl script.
Perl has a set of useful file test operators that can be used to see whether a file exists or not. Among them is -e, which checks to see if a file exists.
Perl uses Perl regular expressions, not POSIX ones. You can compare the syntaxes yourself, for example in regex(7) .
my $file = "test.exe"; # Match a dot, followed by any number of non-dots until the # end of the line. my ($ext) = $file =~ /(\.[^.]+)$/; print "$ext\n";
use File::Basename
use File::Basename; ($name,$path,$suffix) = fileparse("test.exe.bat",qr"\..[^.]*$"); print $suffix;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With