in reply to Parsing out first names

I'm a little late to this party but I would take either of two approaches using regular expressions. The middle initial is optional and occurs at the end of the string so you can match that with (?:\s[A-Z]\.?)?\z and either discard it via substitution or capture a non-greedy match of everything up to it. Like this

use strict; use warnings; my @names = ( q{Mark K.}, q{Bob H}, q{Kurt}, q{Mary Kay K}, q{Mary Jo Z.}, q{Mary Jo}); print qq{Original Substituted Matched\n}, qq{-------- ----------- -------\n}; foreach my $name (@names) { (my $firstNameBySubs = $name) =~ s{(?:\s[A-Z]\.?)?\z}{}; my ($firstNameByMatch) = $name =~ m{^(.*?)(?:\s[A-Z]\.?)?\z}; printf qq{%-12s%-12s%-s\n} , $name , $firstNameBySubs , $firstNameByMatch; }

This produces

Original Substituted Matched -------- ----------- ------- Mark K. Mark Mark Bob H Bob Bob Kurt Kurt Kurt Mary Kay K Mary Kay Mary Kay Mary Jo Z. Mary Jo Mary Jo Mary Jo Mary Jo Mary Jo

Cheers,

JohnGG