in reply to strange behaviour of /regexp/sg

This is a known bug in perl-5.6.1 - the regexp optimiser sees the initial ".*" and treats it as if it were anchored to the start of the string (which is fine), but fails to take into account the /g, which means that when the second match attempt sees that you are not at the start of the string it immediately aborts the match.

Small variations are enough to avoid the optimiser's error: anchoring the pattern with \G is one way:

$testVar =~ /\G(.*?)\n\n/sg
and inserting an empty non-capture is another:
$testVar =~ /(?:)(.*?)\n\n/sg

Hugo

Replies are listed 'Best First'.
Re: Re: strange behaviour of /regexp/sg
by Jaap (Curate) on Mar 21, 2003 at 18:41 UTC
    vote++
    Thank you very much.