G'day ovedpo15,

I think your biggest problem here is that you've immediately reached for a regex solution. When I first saw the question title, before even looking at the content, my first thought was rindex and substr.

Perl's string handling functions (and operators) are, in my experience, substantially faster than achieving the same functionality with regexes. There are times when a regex is appropriate; however, often it's not the best solution.

So, rather than making guesses and assumptions, let's Benchmark.

#!/usr/bin/env perl use 5.010; use strict; use warnings; use constant { PATH => 0, VERSION => 1, WANT => 2, }; use Benchmark 'cmpthese'; use Test::More; my @tests = ( [qw{ /a/b/version1/c/d version1 /a/b/version1 }], [qw{ /some/path/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0/a/b/c 3.5.2+tcl-tk-8.5.18+sqlite-3.10.0 /some/path/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0 }], ); plan tests => 2*@tests; for my $test (@tests) { is _regex(@$test[PATH, VERSION]), $test->[WANT]; is _rindex(@$test[PATH, VERSION]), $test->[WANT]; } cmpthese 0 => { regex0 => sub { _regex(@{$tests[0]}[PATH, VERSION]); }, regex1 => sub { _regex(@{$tests[1]}[PATH, VERSION]); }, rindex0 => sub { _rindex(@{$tests[0]}[PATH, VERSION]); }, rindex1 => sub { _rindex(@{$tests[1]}[PATH, VERSION]); }, }; sub _regex { my ($path, $version) = @_; $path =~ s/.*\Q$version\E\K.*//s; return $path; } sub _rindex { my ($path, $version) = @_; $path = substr $path, 0, length($version) + rindex $path, $version +; return $path; }

The Test::More code is just to ensure the functions are producing correct results; which they are. The output from that is identical in all runs, so I'll just post it once:

1..4 ok 1 ok 2 ok 3 ok 4

I ran the benchmark five times. Here's the sections of output that relate to that:

Rate regex1 regex0 rindex0 rindex1 regex1 809214/s -- -7% -69% -75% regex0 866627/s 7% -- -67% -73% rindex0 2632175/s 225% 204% -- -19% rindex1 3257343/s 303% 276% 24% -- Rate regex1 regex0 rindex0 rindex1 regex1 825777/s -- -5% -68% -75% regex0 870952/s 5% -- -66% -73% rindex0 2579956/s 212% 196% -- -21% rindex1 3261289/s 295% 274% 26% -- Rate regex1 regex0 rindex0 rindex1 regex1 807841/s -- -8% -69% -75% regex0 880657/s 9% -- -66% -73% rindex0 2625422/s 225% 198% -- -20% rindex1 3265104/s 304% 271% 24% -- Rate regex1 regex0 rindex0 rindex1 regex1 807626/s -- -7% -69% -75% regex0 873101/s 8% -- -66% -73% rindex0 2567429/s 218% 194% -- -21% rindex1 3255180/s 303% 273% 27% -- Rate regex1 regex0 rindex0 rindex1 regex1 827447/s -- -6% -68% -75% regex0 877972/s 6% -- -66% -73% rindex0 2579110/s 212% 194% -- -21% rindex1 3260240/s 294% 271% 26% --

Do you still want to go with a regex solution? :-)

— Ken


In reply to Re: How to remove everything after last occurrence of a string? by kcott
in thread How to remove everything after last occurrence of a string? by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.