use warnings; use strict; while () { chomp; s/ ^ [0-9a-z]{3} $ /hi/xi; print "$_\n"; } __DATA__ ABC GHR 1DF uy6 fore