#!/usr/bin/perl @str = ("a,b,c,d,e", "a", "a,b", "a,b,c"); foreach $x (@str){ $x =~ s/\,(\w+)$/ and $1/; print "$x\n"; }