while ($data_in =~ /(.+?)<\/a>(.+?)<\/b><\/container>(<\?query\?>)??/ig) { $data_out = ( defined $3 ) ? "$2, $1$3 " : "$2, $1 "; }