Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

19 Experts available now in Live!

Get 1:1 Help Now