in reply to What is greedy and lazy Matching in perl

Not my forte, but I have a pedantic point to raise; in the context of Perl & regex, the complimentary terms are: 'greedy' and 'non-greedy'.

Ie. 'lazy' does not make sense in this context.


Anyone got any experience of this phone's predecessor?

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
  • Comment on Re: What is greedy and lazy Matching in perl

Replies are listed 'Best First'.
Re^2: What are greedy and lazy matching in Perl?
by Athanasius (Archbishop) on Jul 30, 2015 at 07:39 UTC

    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,

      ... if “lazy” is taken in its more general sense of “doing less work,” then ... frugal quantifiers do less work — and are in that sense “lazier” ...

      But the first definition of the first citation of lazy given here suggests that rather than just passively happening to do less, "lazy" actively avoids effort: "Disinclined to action or exertion; averse to labor; ...; shirking work." This seems to me to capture the essence of the behavior of lazy quantification.

      I can be happy with frugal, but I'm too lazy to type the extra characters. Non-greedy it a bit too Newspeak for me.


      Give a man a fish:  <%-(-(-(-<

        This seems to me to capture the essence of the behavior of lazy quantification.

        The general usage of 'lazy' in the context of programming is: deferred or on-demand; with the complementary term being 'eager'.

        I think that overloading lazy to mean non-greedy for this unique usage, just creates the potential for confusion and unreal expectations.


        Anyone got any experience of this phone's predecessor?

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
        I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
        most, least