my $pat = qr/\w+ ((w)hit(e)|(b)lac(k))/; $_ = 'mostly black'; my($word, $first, $last) = grep defined $_, /$pat/; print "$word starts with $first and ends with $last\n" if $word; $_ = 'but some white spaces'; ($word, $first, $last) = grep defined $_, /$pat/; print "$word starts with $first and ends with $last\n" if $word;