in reply to Pattern matching: Lazy vs. greedy

Hello false_friend, and welcome to the Monastery!

Corion and LanX have answered your specific question, but, in the more general case, you might find it useful to be able to capture all possible matches:

#! perl use strict; use warnings; use Data::Dump; my $string = 'The quick brown fox jumps over the house of the lazy d +og'; my @matches = $string =~ /(?=(the .*? dog))/gi; dd \@matches;

Output:

19:36 >perl 1202_SoPW.pl [ "The quick brown fox jumps over the house of the lazy dog", "the house of the lazy dog", "the lazy dog", ] 19:37 >

You could then select the match(es) you want by greping @matches with suitable criteria. On the look-ahead assertion (?=...), see “Look-Around Assertions” in perlre#Extended-Patterns.

Hope that helps,

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

Replies are listed 'Best First'.
Re^2: Pattern matching: Lazy vs. greedy
by false_friend (Novice) on Mar 30, 2015 at 12:10 UTC
    Hello Athanasius, Thank you for your answer. I tried something like
    my @matches = $string =~ /(the .*? dog)/gi;
    before, which is without the (?= before the regex and the ) after it. This didn’t get me anywhere. I thought I was familiar with look-around assertions, but to be honest, I don’t get what the (?=) is doing here. I thought it is only used as a modification to something preceding it. I consulted http://perldoc.perl.org/perlre.html#Extended-Patterns but could not find why the two variants give different results. Could you explain that to me?
      The point of using /(?=(the .*? dog))/gi here is that look around assertions are zero length, i.e. after they match at position P, the next match is not searched at their end, but at the position P + 1, so you can find overlapping matches, as well.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Thank you. Now I got it.