in reply to Regex problem

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