This is Perl, so there's more than one way of doing this. One is indeed a regexp:
$ext="i_am_the_walrus.txt";
$ext=~s/^[^.]*(\.[\w]+$)/$1/;
print $ext;
A second way is to use substr and index:
use strict;
my $filename="you_are_the_whale.doc";
my $separator=".";
unless (($where=index($filename,$seperator) == -1) {
my $ext=substr($filename,$where);
print $ext;
} else {
print "No extensions in the zoo.\n"; }
The third, and probably nicest way, is to use File::Basename:
use strict;
use File::Basename;
my $filename="we_are_not_all_mammals.zoo";
my ($base, $directory, $ext)=fileparse($filename, '\.[\w]+$');
print $ext;
Beware that the above assumes you're dealing with simple .xyz extensions. For more complicated stuff, see the regexp in
elusion's reply above.
CU
Robartes-
Update: Fixed regexp so it actually works :) (thx elusion). Adapted second recipe to include dot.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.