My perl code:-- $file=~s#((?:[^<]+|<(?!/?tbody>))+(?:\s*(?:[^<]+|<(?!/?tbody>))+)*)#"\n".${TableBody(\$1)}."\n"#gies; sub TableBody { my ($line) = @_; $temp = "$$line"; $temp =~s#$##; $temp =~s#\t##g; $temp ="$temp"; $temp =~s###g; $temp =~s###g; return \$temp; } My input is:-- one two three one two three one two three one two three My output is:-- onetwothree onetwothree onetwothree onetwothree