[plug] Cutting Strings in Perl

Trevor Phillips T.Phillips at murdoch.edu.au
Thu Jan 22 13:12:48 WST 2004


On Thursday 15 January 2004 12:33, Tim White wrote:
> I have a string in perl in the below format.
> C:\dir1\dir2\filename.ext
> (It may have more than 1 extentsion)
> What I wish to do is just get the file name in a number of formats.
> I would like out put in the below formats
> filename
> filename.ext

I'd do it this way:

$foo = 'C:\dir1\dir2\filename.ext';
$foo =~ /^(.*?)(|\.[^\.]+)$/;
$path = "";
$name = $1;
$ext = $2;

if ($name =~ /^(.*)\\(.*)$/)
{
   $path = $1;
   $name = $2;
}

print "Path: $path\n";
print "Name: $name\n";
print "Name w Ext: $name.ext\n";

There's probably a way of combining the two pattern-matches... It's easy if 
there's definitely a path - a bit trickier if there may not be a path...

-- 
. Trevor Phillips             -           http://jurai.murdoch.edu.au/ . 
: Web Technical Administrator     -          T.Phillips at murdoch.edu.au : 
| IT Services                        -              Murdoch University | 
 >--------------------------------------------------------------------<
| On nights such as this, evil deeds are done. And good deeds, of     /
| course. But mostly evil, on the whole.                             /
 \      -- (Terry Pratchett, Wyrd Sisters)                          /




More information about the plug mailing list