How to get part of a path

J

John Smith

Hi,

I'm trying to get the good_part from those directory path.
The beginning is always the same it is "dev/dir1/"

First case : dev/dir1/good_part -> should return good_part
Second case: dev/dir1/good_part/src ->should return good_part
3 case : dev/dir1/good_aprt/obj/html -> should return good_part


I'm able to get each case one at a time but not all of them

dev\/dir1\/(.*) on dev/dir1/good_part => $1 return good_part

dev\/dir1\/(.*)(\/.*) on dev/dir1/good_part/src => $1 return good_part
but don't work on dev/dir1/good_part


Remi
(e-mail address removed)
 
B

Brian Wakem

John said:
Hi,

I'm trying to get the good_part from those directory path.
The beginning is always the same it is "dev/dir1/"

First case : dev/dir1/good_part -> should return good_part
Second case: dev/dir1/good_part/src ->should return good_part
3 case : dev/dir1/good_aprt/obj/html -> should return good_part


I'm able to get each case one at a time but not all of them

dev\/dir1\/(.*) on dev/dir1/good_part => $1 return good_part

dev\/dir1\/(.*)(\/.*) on dev/dir1/good_part/src => $1 return good_part
but don't work on dev/dir1/good_part


Just capture as many characters that aren't a / as possible.

dev/dir1/([^/]+)
 
A

A. Sinan Unur

Hi,

I'm trying to get the good_part from those directory path.
The beginning is always the same it is "dev/dir1/"

First case : dev/dir1/good_part -> should return good_part
Second case: dev/dir1/good_part/src ->should return good_part
3 case : dev/dir1/good_aprt/obj/html -> should return good_part

Please see the posting guidelines to find out how you can make it easier
for people to help you. You made copying and pasting much harder than
necessary.
I'm able to get each case one at a time but not all of them

dev\/dir1\/(.*) on dev/dir1/good_part => $1 return good_part

dev\/dir1\/(.*)(\/.*) on dev/dir1/good_part/src => $1 return good_part
but don't work on dev/dir1/good_part

Show code rather than something that resembles code, but isn't.

This particular problem is easy to solve. For portability, I would use
splitdir and catdir from File::Spec:

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec::Functions qw'catdir splitdir';

my @paths = qw(
dev/dir1/good_part
dev/dir1/good_part/src
dev/dir1/good_aprt/obj/html
);

for my $path (@paths) {
my $good = catdir( (splitdir $path)[2, -1] );
print "$good\n";
}

__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top