Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Match last word in a sentence

by parv (Vicar)
on Aug 14, 2021 at 01:39 UTC ( #11135835=note: print w/replies, xml ) Need Help??

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

Is making the first part non-greedy not enough ...

my $list = "This is my list"; $list =~ m/^(.+?) # The 'rest' (Everything before last word) (\w+) # Last 'word' (string of contiguous word character +s) \W*$ # Possible non-word characters at end of string /x;

... (would it fail on some other string)?

Much later. For English language, \w is not inclusive enough (lacks hyphen) and includes too much (includes underscore & digits). Short of a proper grammar based parser, I would rather use word regex which addresses that ...

$word_re = qr{ (?: & | -? [a-zA-Z]+ [a-zA-Z-]* ) }x;

... is still incomplete as it does not deal with accented characters; periods in a title; acronyms with spaces and/or periods, among other things.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-12-08 03:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found