Dear monks, I have stumbled across an unexpected regex behaviour that I hope someone can explain. More specifically, this is the code:
$text = "<a href='http://www.example.com/'>Example 1</a>\n<a href='htt +p://www.example.net/' target='_blank'>Example 2</a>"; my ($a) = $text =~ /<a href='(.+?)' target='_blank'>/s; print "$a\n";
And this is the output:
http://www.example.com/'>Example 1</a> <a href='http://www.example.net/
I would have expected this, though:
http://www.example.net/
It is not much of a problem as such, as I can always change .+? to [^']+ or remove the /s. What I find a bit odd is that the /s makes the .+? greedy, despite the +?. Could someone please enlighten me?

In reply to Regex becomes greedy with /s by pingo

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.