Update: It appears that this is not an accurate assessment as to what is going wrong. When will I learn to not get involved in regex discussions without testing my theory? ;) This time I'm going to use the excuse that having my backspace key fall off this afternoon has me a little discombobulated. ...off to the repair shop I guess. ;)

Now sit tight a few minutes and watch for someone else to post a correct answer, below.....

?

(I'm not asking a question.)

Your regex contains the submatch construct (.+), which will greedily try to match as much as possible. That means that if the (?:AFFECTED......) submatch can possibly match in several places within the target text, the last one will be the one used, and everything between gets sucked into the greedy .+.

A non-greedy version of .+ is .+?, which provides a cue to the little engine that could(nt) that it needs to match as little as possible instead of as much as possible. Non-greedy matching is not without its own difficulties, but (untested) in this case I think it will help you.

/(?:OVERVIEW|SUMMARY)(.+?)(?:AFFECTED\sPRODUCTS|BACKGROUND)/s # The change is here----^

See any of perlrequick, perlretut, or perlre for an explanation of greedy/non-greedy matching.


Dave


In reply to Re: Regex problem by davido
in thread Regex problem by jayto

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.