Solved

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

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

Accepted Solution

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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