use warnings; use strict; my $li = "abbjghfg table 1 vinoth figer table2"; my $new_str; while ( $li =~ m/(.+?)(table.?\d)/gc ) { $new_str .= $1 . '