in reply to Non-greedy regex behaves greedily

The regular expression solutions will work in the simple cases presented here, but for advanced fixing of HTML tags you may want to look at Text::Balanced and HTML::Parser. For example:
use strict; use Text::Balanced qw(extract_bracketed); my $line = "<body label=\"<<HI>>\"><!--msnavigation-->"; # This will match the brackets properly. my ($match, $remainder) = extract_bracketed($line,"<"); if ($match) { $match =~ s/body/body class=\"theBody\"/; print $match, $remainder,"\n"; } # The regular expression will mess things up. $line =~ s/<body.*?>/<body class="theBody">/is; print $line,"\n";