use warnings; use strict; while () { chomp; my $str = reverse $_; if ( $str =~ m/((.*?)(\s\-\s)?(.*?))\3/ ) { my $matched_str = scalar reverse $1; $matched_str =~ s/^(-\s?)?//; print $matched_str, $/; } } __DATA__ Part Time - Weekend Receptionist - Barton le Clay - Barton-le-Clay (near Luton) Part-Time Weekend Day Veterinary Nurse - Milton Keynes Receptionist - Barton-le-Clay (near Luton) Full Time - Veterinary Nurse - Glasgow Referral - Glasgow Full Time - Professional House wife - Umuezebi Family House - New Haven-Upper-Chime-Enugu ( near 9th Mile ) Full time liar - Glasgow (Australia)