#!/usr/bin/perl use strict; use warnings; my $input_file = "noname1.pl"; open INPUT, '<', "$input_file"; while (my $efile = ) { $efile =~ s/\$var\w \d+ ([!,\#,\",\$,\%,&]) (\w) \$end/\$var $1 $2/; print $efile; } close INPUT; __DATA__ $var1 1 , x $end $varx 2 ! _ $end $vary 3 # aa $end