For curiosity on the performance issue, I have conducted some experiments to compare the performance between a straight regular expression with capture and positive look-ahead with substr & length functions.

The following is my code for the experiment -
use strict; use Benchmark qw/timethese cmpthese/; # use re 'debug'; chomp(my @lines = <DATA>); my $target = shift || 'word'; my $re_positive_lookahead = qr/^a=<(?=.*?word)/; my $re_loose = qr/a=<(.*?$target.*?)>/; cmpthese( timethese(1000000, { 'Match_L' => '&Match_Loose', 'Match_P' => '&Match_PLAhead_plus_Substr', }) ); sub Match_Loose { foreach (@lines) { if (/$re_loose/) { my $word = $1; } } } sub Match_PLAhead_plus_Substr { foreach (@lines) { if (/$re_positive_lookahead/) { my $word = substr($_, 3, length($_)-4); } } } __DATA__ a=<swords> a=<wordy> b=<rappinghood> a=<thisword> b=<thatword> a=<foreword> b=<junk> a=<nothing> b=<word> b=<wordplay> b=<end>
And the result of the experiment is as follows -
Benchmark: timing 1000000 iterations of Match_L, Match_P... Match_L: 31 wclock secs (31.16 usr + 0.00 sys = 31.16 CPU) @ 32092.4 +3/s Match_P: 27 wclock secs (27.64 usr + 0.00 sys = 27.64 CPU) @ 36179.4 +5/s Rate Match_L Match_P Match_L 32092/s -- -11% Match_P 36179/s 13% --
The observation is that positive lookahead combined with substr is about 12% faster than straight regexp with capture, which is not an insignificant speed improvement.


In reply to Re: Removing backtracking from a .*? regexp by Roger
in thread Removing backtracking from a .*? regexp by grinder

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.