in reply to Question on File position, pulling words

Are your variables delineated by whitespace?

To get the last two whitespace-delineated terms out of $_, say

my @last_two = /.*\s(\S+)\s+(\S+)/;

Replies are listed 'Best First'.
Re^2: Question on File position, pulling words
by hipowls (Curate) on Mar 04, 2008 at 06:26 UTC

    Almost correct, you need to anchor the regex at the end of the string.

    my @last_two = $string =~ /(\S+)\s+(\S+)$/;

      Don't overlook my .*. As you can learn in perlretut,

      the first quantifier .* grabs as much of the string as possible while still having the regexp match.

      My submission said

      /.* # Read as far as possible before \s # matching whitespace, (\S+) # capturing a word, \s+ # matching more whitespace, (\S+) # and finally capturing the last word. /x

        True and faster, at least on long strings (I had hoped to riposté by pointing out mine was faster but that is true only on short strings ;-)