You said: or the line starts with \n.

This makes me wonder if part of the problem is conceptual: Generally, better, IMO, to think of \n as the end of a line (and, in the case of the data you present, the end of the previous line).

Combining that notion with the comment above re slurping, would something like this demo be simpler?

my $flag = 0; # TRUE when in a sub header or sub-sub header my @rry = <DATA>; for my $rry(@rry) { if ( $rry =~ /\A={3,4}sub.*?={3,4}$/ ) { $flag = 1; print $rry . "\n"; next; } if ( $rry =~ /\A[^=].*/ && $flag == 1 ) { print $rry . "\n"; } else { $flag=0; next; } }

... and it seems to work. Output with your sample as __DATA__

===sub header=== [[testing]] ====sub-sub header==== bla bla ===sub header=== [[testing]] sub-sub header bla bla

For your purposes, this (slightly modified) might well be in a sub, triggered by a match on more than two "=" signs.

Update:   Attributed quote in the first graf and added last para.


In reply to Re: Regex question - either starts, or has newline prefixing? by ww
in thread Regex question - either starts, or has newline prefixing? by ultranerds

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.