use warnings; use strict; while (my $line = ) { # Assume one "element" per line if ($line =~ /<(\w+)>(.*?)<\/\1>/) { (my $midstr = $2) =~ tr/<>/[]/; $line = "<$1>$midstr\n"; } print $line; } __DATA__ my data here further text not good as contains in angle brackets another possibility is one in here