http://qs1969.pair.com?node_id=11135834


in reply to Re: Match last word in a sentence
in thread Match last word in a sentence

This appears to contain a bug, revealed when you change the print lines as shown below:

use strict; use warnings; my $list = "This is my list"; $list =~ / ^(.+) # The 'rest' (Everything before last word) (\w+) # Last 'word' (string of contiguous word characters) \W*$ # Possible non-word characters at end of string /x; my $last = $2; my $the_rest = $1; print "the_rest='$the_rest'\n"; print "last='$last'\n";
Running this produces:
the_rest='This is my lis' last='t'

There are many ways to fix. Here is one way (adding a \b assertion):

$list =~ / ^(.+) # The 'rest' (Everything before last word) \b(\w+) # Last 'word' (string of contiguous word characters) \W*$ # Possible non-word characters at end of string /x;

Alternative fixes welcome.