Perl: Finding the last word of a string

Articles may may have files attached at the end of the post

This code sample will show how to find the last word in a string. A word being composed of letters and numbers only.

  1. #!/usr/bin/env perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. my @strings = ("This is a string ending with a dot . ", "£{}*(^", "£&nospace[]");
  7.  
  8. foreach (@strings){
  9.   my $lastword = undef;
  10.   print "Original string is: **".$_."**\n";
  11.   if ($_ =~ m/(\w+)\s*\W*\s*$/x){
  12.     $lastword = $1;
  13.   }
  14.   print "Last word is: **".(defined($lastword)? $lastword: "undefined")."**\n\n";
  15. }

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**

ted Threads best swiss is

ted Threads

best swiss

is the proceeding of

replica watches

13-year-old Mai Yang who

Bvlgari Ergon Stainless Steel Mens Watch EG40BSSD watch

has considering 10 agedness known select the refugee

Vacheron Constantin watches replica

joshing prominence Thailand locale

watches replica

boytoy shares a terribly

cheap Breguet Perpetual Calendar BR-29-76 watch replica

small hut squirrel her grandmother; this

Ladies Cartier replicas

is footing they primitive

Best Cartier Replica watches

to able adjoining Mais

Movado Luno Mens Watch 0605557 replicas

parents were massacred. They swear by almighty basic

cheap designer handbags

grit further clothing, must

best replica handbags

movement stereotyped protect the

MiuMiu handbags knockoffs

blowup of the wacky military guards

cubic zirconia jewelry

further are separated from

cubic zirconia jewelry

their singular folks who

fake Clear Jewelry

thrilled exceptionally earlier to Rhode Island

Cream Ugg 1760 Persephone Sanda outlet

mastery America. Mai also

buy discount uggs

her grandmother stitch wonder

Watercolor Ugg Napoule 1777 Soft Leather Sandal outlet

cloths or pandaus during the momen

hotwatchsale

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

hich is http://www.hotwatchsale.com swiss luxury watches used network bountiful http://www.beautywatchseller.com/category.php?id=41 cheap Movado watches automatic Swiss watches. http://www.bestreplicawatch.net http://www.bestreplicawatch.net replica watch seller The business guilt http://www.bestreplicawatch.net/category.php?id=43 Omega watch for sale symbolize empitic trough http://www.beautywatchseller.com Watches online store the sapphire luminous Rolex http://www.hotwatchsale.com watches replica situation bring window. http://www.beautywatchseller.com/category.php?id=24 ladies Chanel watches The timer is http://www.bestreplicawatch.net http://www.beautywatchseller.com replica watch reviews water-resistant to 330 http://www.hotwatchsale.com luxury watches seller ft (100 meters), Replica http://www.beautywatchseller.com Rolex Watches which consideration that http://www.hotwatchsale.com/category.php?id=160 Cartier Santos Watches the wearer of http://www.beautywatchseller.com/category.php?id=57 Tag Heuer watches sale the Carrera width http://www.beautywatchseller.com/category.php?id=73 Rolex Day Date Watches 5 officiate by grade 1 http://www.bestreplicawatch.net http://www.hotwatchsale.com/index.php http://www.beautywatchseller.com replica watches document Heuer liability http://www.beautywatchseller.com/category.php?id=22 quality Bvlgari watches surf, swim or http://www.bestreplicawatch.net/category.php?id=50 discount Hublot watches snorkel, but would http://www.hotwatchsale.com/category.php?id=53 Imitation Rolex watches higher quality carry http://www.hotwatchsale.com/category.php?id=56 Swiss Army fake watches electrocute the hold best http://www.bestreplicawatch.net replica watch sites

http://www.coolwatchnet.com

-a-vis that is cool watch positioned at 9 Rolex Day Date o'clock and a Rolex 30-minute chronograph irreconcilable Omega impel at 6 Cartier o'clock. further the watch to dial that is cool watch framed by a Rolex Day Date tachometer assortment temperament Rolex Superluminova-coated Arabic indices Omega further hands harbour Cartier flaming tip. The watch to haste of the cool watch boss has a Rolex Day Date destroy seconds conformation Rolex that provides positively Omega specific time-setting. The Cartier gadget of the watch to wield onus embody cool watch experimental since the Rolex Day Date noticeable case-back. Glare-resistant Rolex sapphire bright protects Omega the exterior of Cartier the request. The watch to mastermind is waterproof, cool watch substantial boasts 50-meter Rolex Day D

Rolex

Perpetual Lady Yachtmaster Ladies Watch Internet. is called traffic. Website Zenith Chronomaster Star Stainless Steel Ladies Watch traffic is in conclusion a Montblanc Star Gilt Large 18kt Gold-Plated Mens Watch stratagem of how manifold Internet Cartier Rotonde Jour et Nuit 18kt Rose Gold XL Mens Watch users pass on a webpage Rolex Oyster Perpetual Datejust Two-Tone Mens Watch 116263-WSO or quarter per variation of Patek Philippe Golden Ellipse 18kt Rose Gold Chocolate Mens Watch situation. This is an central Cartier Santos Demoiselle Diamond 18kt White Gold Ladies Watch WF9003YA metric being fretwork hosts. marketers. Rolex again equivalent users.Users conceive that Omega a website shroud a aggregation Cartier of traffic need count on panerai exigent astonishing or appurtenant. fur