Regexp question...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi All,

Apologies in advance if this is a dumb question ("There are no stupid
questions, but there are loads of inquisitive idiots")...

I wanted a regular expression to seperate a filename from a full path
(UNIX). I came up with the following:

$target =~ m%(.+/)(.+)%;
$targetpath = $1;
$targetfile = $2;

Actually, I expected that not to work; but it does, apparently
perfectly. I thought it would fail because I thought the $1 portion of
the regexp would process $target until it found the leftmost '/' and
then stop, leaving everything else to the $2 part. Why am I wrong? Am I
just feeding it a particularly restrictive set of cases?

What regular expression could I use if I wanted to split a UNIX path
into the bottom level (that is, closest to root) directory and the rest
of the path?

Thanks for your indulgence.


Re: Regexp question...

* schrieb:
Quoted text here. Click to load it

In regular expressions ".+" is greedy. Hence the first part process
$target until the *rightmost* slash is found.

Quoted text here. Click to load it

Avoid that greediness. Please read `perldoc perlre` for details.

    $target =~ m%(.+?/)(.+)%;


Re: Regexp question... wrote in

Quoted text here. Click to load it

You do not need the full regex machinery to do accomplish such a
task. In addition, using File::Basename is more appropriate than
rolling your own in this case. See




(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:

Site Timeline