Perl: Finding the last word of a string
Articles may may have files attached at the end of the post
Submitted by chantra on Mon, 04/07/2008 - 20:43
This code sample will show how to find the last word in a string. A word being composed of letters and numbers only.
- #!/usr/bin/env perl
- use warnings;
- use strict;
- my @strings = ("This is a string ending with a dot . ", "£{}*(^", "£&nospace[]");
- foreach (@strings){
- $lastword = $1;
- }
- }
Will output:
$ ./lastword.pl
Original string is: **This is a string ending with a dot . **
Last word is: **dot**
Original string is: **£{}*(^**
Last word is: **undefined**
Original string is: **£&nospace[]**
Last word is: **nospace**












