Note that my two solutions aren't identical. Given your pattern of / \w\w /, you'd want to use /.*($PATTERN)/ not ( /($PATTERN)/g )[-1] because the second case wouldn't DWIM with "overlapping matches":

my $last= ( " AB CDE FG HI JKL " =~ /( \w\w )/g )[-1]; print $last, $/; __END__ prints " FG " (not " HI ")

You could "fix" my second technique by switching the pattern to /(?<= )\w\w(?= )/ so that matches never overlap.

But that doesn't make the second choice "better". There are also patterns where the second technique won't DWIM but the first technique will. In particular, patterns that need to be greedy need the first technique or else they won't be greedy:

my( $last )= " AB CDE FG HI JKL " =~ /.*(\w+)/sg; print $last, $/; __END__ prints "L" (not "JKL")

Which begs one to ask what technique will get the "last match" and DWIM in the face of a greedy pattern with overlapping matches.

my $string= " AB CDE FG HI JKL "; my $PATTERN= qr/(?! F)(?: \w+){1,3} /; # (match space-separated words where the first doesn't start with "F") my( $last )= $string =~ /.*($PATTERN)/sg; print $last, $/; $last= ( $string =~ /($PATTERN)/g )[-1]; print $last, $/; __END__ prints: JKL JKL never: HI JKL

This illustrates an important point about regular expressions. They prefer to be greedy but more than that they prefer to match left-most (that is, to match earlier in the string rather than further to the right in the string). That is why texts on regexes say "greedy" as "left-most longest" (and not "longest left-most").

So I've hypothesized a case where we first want the match to end as far right as possible and second want the match to start as far left as possible. One way to get this from the regex engine is to use a "sexeger" (reverse the string and compose the regex backward), a technique coined by japhy:

my $string= reverse " AB CDE FG HI JKL "; my $PATTERN= qr/(?: \w+){1,3}(?<!F) /; # (match space-separated words where the last doesn't end with "F") my $last= $string =~ /($PATTERN)/ ? $1 : undef; print reverse($last) . $/; __END__ prints " HI JKL " !

You could also get it with more "brute force":

my $string= " AB CDE FG HI JKL "; my $PATTERN= qr/(?! F)(?: \w+){1,3} /; # (match space-separated words where the first doesn't start with "F") my $last; my $end= -1; while( $string =~ /(?=($PATTERN))/g ) { my $stop= $+[-1]; if( $end < $stop ) { $last= $1; $end= $stop; } } print $last, $/; __END__ prints " HI JKL " !

- tye        


In reply to Re^3: Get the last occurence of a pattern (more) by tye
in thread Get the last occurence of a pattern by nandn

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.