Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a regular expression in Perl to find a file's extension?

Tags:

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"?

like image 481
Zerobu Avatar asked Mar 18 '10 01:03

Zerobu


People also ask

How do I search for a pattern in Perl?

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.

What is the extension of Perl files?

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.

How do I check if a file exists in Perl?

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.

What regex does Perl use?

Perl uses Perl regular expressions, not POSIX ones. You can compare the syntaxes yourself, for example in regex(7) .


2 Answers

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"; 
like image 145
Gavin Brock Avatar answered Sep 29 '22 16:09

Gavin Brock


use File::Basename

  use File::Basename;   ($name,$path,$suffix) = fileparse("test.exe.bat",qr"\..[^.]*$");   print $suffix; 
like image 27
ghostdog74 Avatar answered Sep 29 '22 15:09

ghostdog74