There's 2 ways to go about it...

if you always have file names, you should really use the File::Spec module
(which has been core for quite some time)
if you really want to use a regexp, well be my guest, but the above method
is really better...

i'll show you both:

### the script ###

use strict;
use File::Spec;

### the string you had.. note the \\ : a \ escapce the next char, so a \\
'translates' to a single slash in the actual string
my $str = "C:\\PerlScripts\\TestSamples\\StringTest.pl";

### the regexp way:
### match on a literal \ followed by any word character (a-zA-Z_) and a .
followed by the end of the line
my ($file1) = $str =~ m|\\([\w.]+)$|;

### or the easy way: use file::spec->splitpath
my ($volume,$directories,$file2)  = File::Spec->splitpath($str);

print "file1: $file1, file2: $file2\n";

###

print them out, you'll see it yields the same.
a note to make: File::Spec->splitpath will work on any platform.. your regex
will have to be modified to work on windows/unix/macos

hth,
jos


----- Original Message -----
From: "David" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, January 06, 2002 5:01 PM
Subject: Using regexp to get a substring


> Hello All,
>
> I am beginner and need some helps. Thanks a lot!
>
> The question is, if I have a string, for example
> "C:\PerlScripts\TestSamples\StringTest.pl", how do I use regexp to parse
> this string and get substring after the last backslash ("StringTest.pl").
>
> Thanks in advance!
> David
>
>
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to