my $xml = "one\ttwo\tthree\n"x40; $xml =~ s#((?:[^<]+|<(?!/?tbody>))+(?:\s*(?:[^<]+|<(?!/?tbody>))+)*)#"\n".${TableBody(\$1)}."\n"#gies; print $xml; sub TableBody { my ($line) = @_; $temp = "$$line"; $temp =~s#$##; $temp =~s#\t##g; $temp ="$temp"; $temp =~s###g; $temp =~s###g; return \$temp; } #### my $xml = "one\ttwo\tthree\n"x5; $xml =~ s#(.*?)#${TableBody(\$1)}#gies; print $xml; sub TableBody { my $line_ref = shift; my $return_xml; foreach my $thing (split(/\t/, $$line_ref)) { $return_xml .= "$thing"; } return \"$return_xml"; }