use strict; use Text::Balanced qw(extract_bracketed); my $line = ">\">"; # 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///is; print $line,"\n";