Oh right. Well it was a quickie answer anyway (at least I got the explanation right). I'd avoid /g there though since the OP might accidentally call it in list context and screw it all up. I'd probably just do the later match against a substr() lvalue like my $end = $start + (substr( $str, $start ) =~ /b/ ? $-[0] : 0);. Roughly like that anyway. I didn't mention that I tested none of this - I'm just writing it and figuring that I know how to speak perl correctly.

I also have an aversion to pos() since I know that its behaviour is currently undefined with regard to local(). In this case that "bug" isn't relevant so I suppose I could go along with a use of pos(). Exactly what you'd do with it though... I dunno.

# Maybe this. I think I'd rather just access $+[0] directly. my $start = $str =~ /a/g ? pos $str : 0; my $end = $str =~ /b/g ? $-[0] : 0; my $length = $end > $start ? $end - $start : 0; my $ext = $length ? substr( $str, $start, $length ) : undef;

In reply to Re: Re: Re: print data between two regular expressions by diotalevi
in thread print data between two regular expressions by Anonymous Monk

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.