Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-06-26
2
Medium Priority
?
336 Views
Last Modified: 2013-06-26
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

Open in new window


Return:

here

Open in new window


or

\some\path\in\here

Open in new window


Return:

here

Open in new window


or

\some\path/in/here

Open in new window


Return:

here

Open in new window


or

/some/path\in\here

Open in new window


Return:

here

Open in new window

0
Comment
Question by:Tolgar
2 Comments
 
LVL 27

Assisted Solution

by:wilcoxon
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];

Open in new window

Or this:
if ($str =~ m{^.*[/\\](.*)$}) {
    $last = $1;
}

Open in new window

0
 
LVL 85

Accepted Solution

by:
ozo earned 1000 total points
ID: 39278808
($last) = $str =~ m{([^/\\]*)$};
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question