Your index() solution does not do exactly what the regex solution does. That may or may not make a difference in this application, but I think worth pointing out. The regex is "greedy" and will wind up matching the last occurrence of $version. Your index() code will find only the first occurrence of $version.

Try tybalt's code with:

$path = "/some/path/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0/a/b/3.5.2+tcl-tk +-8.5.18+sqlite-3.10.0/c"; # and you will see that only the last "/c" is deleted. /some/path/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0/a/b/3.5.2+tcl-tk-8.5.18+s +qlite-3.10.0
As far as maintainability and understandability goes, I would not use the \K and prefer the more common way:
instead of : $path =~ s/.*\Q$version\E\K.*//s; I probably would have coded: $path =~ s/(.*\Q$version\E).*/$1/s;
I think \K is specific to Perl or at least I know that it does not exist in some other regex dialects that I use.

From the use case presented, the speed of execution doesn't matter at all. I would opt for simplicity and avoid uncommon things like \K. I could code a faster, better version of your index() approach in ASM and it would run like a "super rocket" but to absolutely no effect whatsoever upon total program execution time. And I think this could miss use cases involving wide characters which the regex will handle as part of Perl (the one byte per character assumption although extremely useful for many things, it does have some limitations).

I don't know why the /s regex modifier is used and the rationale behind that could be a bit obscure? Normally "." matches anything except \n. /s allows "." to include the "\n". I would not expect to see an \n in a path name. I'm not sure that this makes any difference at all, but again, some of these small things can matter depending upon the circumstances.


In reply to Re^2: How to remove everything after last occurrence of a string? by Marshall
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.