http://www.perlmonks.org?node_id=324190

diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

use Term::ANSIColor ':constants'; # Note that there is not normally a space betweeh the </ and code>. I +added that so that Perlmonks.org wouldn't parse the post incorrectly. $_ = "normal <code> green <!-- yellow [red] normal --> normal </ code> + normal"; $GREEN = GREEN; $YELLOW = YELLOW; $RED = RED; $RESET = RESET; s(<code>(.+?)</ code>)($GREEN$1$RESET)g; s((?<=\x1b)\[(.+?)\])($RED$1$RESET)g; s((<!--.+?-->))($YELLOW$1$RESET)g;

The preceding program produces the following string. Overlapping is not detected and each markup element terminates the enclosing element prematurely.

"normal " . GREEN . " green " . YELLOW . "<!-- yellow " . RED . "red" . RESET . " normal -->" . RESET . " normal" . RESET . " normal"

I'd like it to produce this. If perl had variable length look-behind I'd write this as (?<=(?:^|$RESET)[^\x1b]*). Any ideas for how to write this without using perl's experimental regexp features ( (?{ code }), (??{ rx }), and (?(expr)true|false))?

"normal " . GREEN . " green <!-- yellow [red] normal --> normal" . RESET . " normal"