Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Two things that haven't been mentioned yet:

I guess the regular expression needs to start from right to left correct?

The regex engine always operates from left to right. The solutions provided here using the $ anchor don't change that - the regex engine is still operating from left to right, but it's only matching on the last word because of the anchor.

Trying to get the last word in a sentence

Note that "This is my list" is missing punctuation. It's unclear from your question if your sentences are already split, but if you were to process text like "I took the medicine that Dr. Wall recommended to me. It tasted like onions.", you'd want that to be split into two sentences, not three - modules like Lingua::Sentence will do that for you.

use warnings; use strict; use Lingua::Sentence; use Data::Dump; my $text = "I took the medicine that Dr. Wall recommended to me. It ta +sted like onions."; my $splitter = Lingua::Sentence->new("en"); my @sentences = $splitter->split_array($text); dd @sentences; for my $sentence (@sentences) { if ( $sentence =~ /\s(\w+)[^\w\s]?$/ ) { dd $1; } else { warn "Failed to get last word from: $sentence" } } __END__ ( "I took the medicine that Dr. Wall recommended to me.", "It tasted like onions.", ) "me" "onions"

(This uses the somewhat simplistic [^\w\s] to try and match any final punctuation, it may need to be adjusted depending on the input data.)


In reply to Re: Match last word in a sentence by haukex
in thread Match last word in a sentence by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-23 17:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found