Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Match last word in a sentence

by hippo (Bishop)
on Aug 13, 2021 at 14:17 UTC ( [id://11135819]=note: print w/replies, xml ) Need Help??


in reply to Match last word in a sentence

Your code as posted does not compile:

$ perl -cw 11135816.pl No such class list at 11135816.pl line 6, near "my list" syntax error at 11135816.pl line 6, near "my list =" syntax error at 11135816.pl line 9, near "$1 print" Global symbol "$the_rest" requires explicit package name at 11135816.p +l line 10. Bareword "list" not allowed while "strict subs" in use at 11135816.pl +line 6. 11135816.pl had compilation errors.

Cleaning it up and fixing the regex by anchoring to the end and allowing spaces in the first group we have:

use strict; use warnings; my $list = "This is my list"; $list =~ /(.+)\s(\w+)$/; my $last = $2; my $the_rest = $1; print $the_rest . "\n"; # "This is my" print $last . "\n"; # "list";

🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135819]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2024-04-19 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found