to elimitate the extra space that's left when the word is removed. This leaves us with:$sentence =~ s/\b\Q$word\E\b\s+(?!.*\b\Q$word\E\b)//s;
where the $word = "dog".String: the quick brown dog jumped the doggy style dog killing doggie +eater. Result: the quick brown dog jumped the doggy style killing doggie eate +r.
(Added by tye) Note that if the last occurance of that word is also the last word of a sentence, it will have whitespace in front of it and not behind it and this regex will fail. If the last occurance of the word is unlikely to be the first word in the string, then
would be a better choice.$sentence =~ s/\s*\b\Q$word\E\b(?!.*\b\Q$word\E\b)//s;
In reply to Re: How do I delete the last instance of a word from a string?
by Incognito
in thread How do I delete the last instance of a word from a string?
by Warped
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |