For a good start on greedy regexps see Ovid's classic Death to Dot Star!. Essentially the greediness of a regexp relates to the fact that both * and + will match the preceding atom for as long as possible. However you can alter this behaviour by appending either modifier with a question mark e.g
my $str = "123 foo 456 foo 789 foo 10"; # will match everything up to the last 'foo' print "dot star greedy: ", $str =~ /(.*)foo/, $/; # will match everything up to the first 'foo' print "dot star non-greedy: ", $str =~ /(.*?)foo/, $/; __output__ dot star greedy: 123 foo 456 foo 789 dot star non-greedy: 123

HTH

_________
broquaint

update: made code more illustrative


In reply to Re: A query on greedy regexps by broquaint
in thread A query on greedy regexps by sch

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.