Solved

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

Posted on 2013-06-26
324 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 26

Assisted Solution

wilcoxon earned 250 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 84 Accepted Solution ozo earned 250 total points ID: 39278808 ($last) = $str =~ m{([^/\\]*)$};
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans