开发者

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

开发者 https://www.devze.com 2023-02-26 05:48 出处:网络
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 r

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


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.


/\s?dog[\s\.]*$/

Sample at http://refiddle.com/10i

0

精彩评论

暂无评论...
验证码 换一张
取 消