use strict; use warnings; my $s = "SELECT f1,f2,f3,f4 as x2, (f4= f5) as x3 FROM ...." ; $s =~ s/,\s*.*?\s+as/,/gi ; print "OUT: $s\n";