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:
Running this produces: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";
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Match last word in a sentence
by parv (Parson) on Aug 14, 2021 at 01:39 UTC |
In Section
Seekers of Perl Wisdom