Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Everything after trailing slash...

Status
Not open for further replies.

pat57

Programmer
May 27, 2005
3
GB
Hi,

I'm not a perl programmer, but I can chop and change things when I find the right code. On this occasion I need to do just that but can't find it, I hope someone can help me!

It's pretty simple I think. Basically I have a string which consists of "One thing/The second thing/The Third Thing" and want to extract everything after the last slash, i.e "The Third Thing". There may be one slash or hundreds of slashes in the string. Some might not have any slashes though so there needs to be an if statement to check... i.e. if the string was "Hello" the output should be "Hello" :)

Thanks very much in advance,

Pat
 
Two methods:

Code:
$string = 'One thing/The second thing/The Third Thing';
$position = rindex($string, "/") + 1;
$ending = substr($string, $position);
$path = substr($string, 0, $position);
print "$path\n";
print"$ending\n";

or alternatively with a regular expression:

Code:
$string=~m/([a-zA-Z0-9\s]+)[.!?]?\s*$/x;
print "$1\n";
 
Thank you both very much indeed, it works and that has sorted my dilemma :)
 
Sorry guys, I think I was talking total rubbish!
LOL
maybe this might have been better.
Code:
m/\/?([^\/]+)$/ and print $1;


Trojan.

 
Never mind, I used the one that did work, heh :)

Thanks for the clarification!
 
if all you need is the last part after the slash for a lot of line (in a file or array) you can do something like this:

Code:
my @strings = (
'One thing/The second thing/The Third Thing',
'hello',
'another/string'
);
print substr($_,rindex($_, "/")+1)."\n" for @strings;
 
What's wrong with something as easy as this? I'm no regex master, just curious

Code:
m[./(.*)$];
 
besides the fact that it will not return the desired results, there is technically nothing "wrong" with it. Even if it written to return the correct results it is still slower than using substr and index to achieve the desired output:

Code:
#!perl 
use strict;
use Benchmark qw(:all) ; 
use CGI qw/:standard/;
print header;
print "<plaintext>";
cmpthese(10000, {


'substr' => sub {

   my @strings = (
   'One thing/The second thing/The Third Thing',
   'hello',
   'another/string'
   );
   print substr($_,rindex($_, "/")+1)."\n" for @strings;

},

'regexp' => sub {

   my @strings = (
   'One thing/The second thing/The Third Thing',
   'hello',
   'another/string'
   );
   for (@strings) {
      /^(?:.*\/)?(.*)$/;
      print "$1\n";
   }
},
});

results:

Code:
          Rate regexp substr
regexp  8333/s     --   -36%
substr 12987/s    56%     --

the results may vary from computer to computer and my test machine is a relatively slow 700mhz cpu.
 
Yes, my first one doesn't work.

But this is a great alternative. Instead of matching too much, just remove everything before the last flash and just resave?

Code:
$string =~ s/.+\///;

This is what I'd use regardless of speed. It's a lot easier, less typing, looks clean and if chances are the speed difference is only noticible by the pc.
 
I guess as long as you understand the difference then its a personal choice to use this code or that code. But I hope you are not reaching the wrong conclusion: that a line of code that is shorter will always be best (or faster). I look at this block:

Code:
print substr($_,rindex($_, "/")+1)."\n" for @strings;

and it's just as easy to understand as this block:

Code:
for (@strings) {
   /^(?:.*\/)?(.*)$/;
   print "$1\n";
}

and they both do the exact same thing but one does it better than the other. Hopefully most programmers will choose the most efficient methods so programs run as fast and efficient as they can.

Nuff said. [peace]
 
Yeah well.. for those of us who don't read Chinese* like that, smaller regexes are so much nicer. LOL.

Your regex is easier to read than your substring but the ?: keeps confusing things whenever I come across it :)




* disclaimer: Chinese was not meant to be an insult to the Chinese language but to describe how difficult these regexes can get
 
(?: ..... ) in a regex is the same as normal brackets except it does not capture the text it matches.
Usually you use it to improve speed and/or save backreferences.


Trojan.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top