Solved

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

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

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
Comment Utility
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 84

Accepted Solution

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now