Hello BrowserUk,

I don’t think it’s pedantic (in the pejorative sense, anyway) to insist on correct terminology. Often, when one is trying to understand a new technical concept, getting the terminology clear is half the battle. For regex quantifiers, the distinction is between greedy, on the one hand, and non-greedy or frugal (see below) on the other. And “lazy” does suggest lazy evaluation, which has no application here.

But... if “lazy” is taken in its more general sense of “doing less work,” then it may be worth noting here that frugal quantifiers do less work — and are in that sense “lazier” — than their greedy counterparts:

use strict; use warnings; use Benchmark 'cmpthese'; my $date = '(30-Jul-2015)'; my $string = 'A' x 1e5 . $date . 'B' x 1e5; cmpthese ( 1e4, { Greedy => sub { $string =~ / ^ .* ( \( \d{2} - \w{3} - \d{4} \) + ) /x; $1 eq $date or die $!; }, Frugal => sub { $string =~ / ^ .*? ( \( \d{2} - \w{3} - \d{4} \) + ) /x; $1 eq $date or die $!; }, } );

Typical output:

17:36 >perl 1323_SoPW.pl Rate Greedy Frugal Greedy 1068/s -- -70% Frugal 3555/s 233% -- 17:37 >

Of course, this information will be useful only for those situations in which it can be known in advance that a frugal match is guaranteed to produce the same result as its greedy equivalent. So far I haven’t thought of any practical examples that qualify. But “no knowledge is ever wasted,” as my mother always says.

Yours in pedantry, :-)

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^2: What are greedy and lazy matching in Perl? by Athanasius
in thread What is greedy and lazy Matching in perl by shankonit

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.