Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help in regex

Status
Not open for further replies.
May 3, 2003
90
CA
Hi All,

Im having a problem with regex.

I am trying split the filename from the path name.

EX:

c:\blablabl\somedr\thefile.txt

I want the file name "thefile.txt" to be extracted.
I already had done this a while ago but I can't find the file. I used the "m/"pattern matching. Please help or point in the right direction.

Thanks
 
$x = 'c:\blablabl\somedr\thefile.txt';

@a = split(/\\/, $x);
$name = pop(@a);

print $name;
 
Code:
#!perl
use strict;
use warnings;

my $path = 'c:\blablabl\somedr\thefile.txt';
my $filename = $1 if $path =~ /\\([^\\]+)$/;
# or: my ($filename) = $path =~ /\\([^\\]+)$/;
print "$filename\n";
 
Although a solution such as that may work ok for your purposes, it's not portable and you'd have to revise your code if you ever moved it to another OS. You'd be better off using the standard File::Basename module for this - it may save headaches in the future.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top