[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Easy regex?



>	Hi All
>
>	I want a directory path to the directory the script is in, but the
>env. var. SCRIPT_NAME returns a path with the file name on the end so I
>wonder if someone can help with an easy regex, (or maybe suggest a method
>that'll return the directory)  to truncate a directory path by chopping off
>the filename at the end, eg
>
>> $orig_path =
>> "server.mydomain.de/directory1/directory2/irritating_filename.pl";
>> 
>> and I want 
>> 
>> $desired_path ="server.mydomain.de/directory1/directory2/";
>> 
>> the number of directories and their names is variable; the name and of the
>> irritating file and the length of it's name may change, but for the time
>> being assume *.pl

Hi Scott,

At the risk of offending the regex gods <GRIN>, I wouldn't use a regex at all...


try:

## return all characters up to the first /  starting from the end 
$desired_path=substr($orig_path,0,rindex($orig_path,'/')+1);

if you _have_ to use a regex, something like:

## all characters up to the first / ([^\/]) starting from the end ($)
$orig_path=~/([^\/]*)$/;
$desired_path=$`;

cheers

Iain

(whos going to work out how to use the benchmark module real soon now :-)

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch