in reply to Stumped by regex

I'm not sure how to do this exactly with a regex but one could use something like: -
use strict; my @names = ( 'Joe John Smith', 'David George', 'Emma Harry Sally Martin', ); foreach (@names) { my @w = split /\s/, (/\s+(\w[\w ]{0,18}\w)\s*$/)[0]; shift @w while @w > 2; print "@w\n"; }
Update: You can replace everything in the foreach scope with: -
(/\s+(\w[\w ]{0,18}\w)\s*$/)[0] =~ /(\w+\s?\w+)$/g;