go ahead... be a heretic | |
PerlMonks |
Re: Match last word in a sentenceby haukex (Archbishop) |
on Aug 15, 2021 at 10:20 UTC ( [id://11135851]=note: 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.
(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 Section
Seekers of Perl Wisdom
|
|