Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found