Solved

# How to catch the part after the last slash in Perl?

Posted on 2013-06-26
Medium Priority
343 Views
Hi,
How can I catch the last part of a string which is after the last slash (back slash or forward slah) in Perl?

Example:

/some/path/in/here


Return:

here


or

\some\path\in\here


Return:

here


or

\some\path/in/here


Return:

here


or

/some/path\in\here


Return:

here

0
Question by:Tolgar

LVL 27

Assisted Solution

wilcoxon earned 1000 total points
ID: 39278652
Assuming the string you want to extract the last part from is in $str, this will do it: $last = (split m{[/\\]}, $str)[-1];  Or this: if ($str =~ m{^.*[/\\](.*)$}) {$last = $1; }  0 LVL 85 Accepted Solution ozo earned 1000 total points ID: 39278808 ($last) = $str =~ m{([^/\\]*)$};
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.