It looks like you're trying to find the filename (without extension) from a fully-qualified file path. If this is the case, then look into the File::Basename
core module:
my $str = "/somedir/ref/some-dir/foo.word";
my( $filename, $directory, $suffix ) = fileparse($str, qr/\.[^.]*/);
The fileparse()
method takes two arguments: the string to be parsed and the file suffix to be removed. If you don't know what the file suffix is going to be beforehand, then you can supply a regular expression. In this case, the suffix will match a period followed by zero or more non-period characters.
Edit: And if you're not finding filenames, and want the letters between the last /
and the last .
, try this:
my $str = "/somedir/ref/some-dir/foo.word";
my @elems1 = split '/', $str;
my @elems2 = split '\.', $elems1[-1];
my $foo = $elems2[-2];
TIMTOWTDI! :-)