Mike
Mike

Reputation: 35

How to use a Regex to remove a word only when it occurs at the end of a string (for Yahoo Pipes)?

I need a Regex that will look at the last word in the string and eliminate it if it's a word that I've selected. For instance, if I'm selecting the word "dog," "This dog is a great dog" would return "This dog is a great." I don't want it to affect all the instances of that word, only if it happens to be at the very end of a string.

This is for a Yahoo Pipe that I'm setting up. Thanks in advance for your help. -Mike

Upvotes: 1

Views: 3196

Answers (2)

Paul Alexander
Paul Alexander

Reputation: 32367

/\s?dog[\s\.]*$/
  • 1 optional whitespace
  • dog
  • 0 or more: whitespace or .
  • End of line

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336178

The regex \bdog$ matches only at the end of the string. If there can be whitespace after your keyword, try \bdog\s*$. If you want to allow other characters (except for alphanumerics) after dog, for example punctuation, then use \bdog\W*$.

\b is a word boundary anchor that makes sure that only an entire word dog is matched - not part of a word as in underdog.

\s matches whitespace.

\w matches an alphanumeric characters; \W matches anything that's not an alnum.

Upvotes: 4

Related Questions