Joey
Joey

Reputation: 7

Capture sub-directories from path

my $path= "/tmp/main/store/important/file/log"; ##string

$path =~ m/file/;

print "$&/$'\n";

I want to capture file/log/.*/.*/ .. . . ..

And the path may vary, any other way to solve this?

Thanks guys

Upvotes: 0

Views: 46

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 149736

Just use greedy matching (.*):

my $path= "/tmp/main/store/important/file/log";
my ($tail) = $path =~ m!(/file/.*)!;
print $tail;  # /file/log

Here I also use a custom delimiter for the m// operator to avoid escaping slashes.

Upvotes: 2

Related Questions