如何使用正则表达式仅删除出现在字符串末尾的单词(对于雅虎管道)?
我需要一个正则表达式来查看字符串中的最后一个单词,如果它是我选择的单词,则将其删除。例如,如果我选择“狗”这个词,“这只狗是一只很棒的狗”将返回“这只狗是一只很棒的狗”。我不希望它影响该单词的所有实例,除非它恰好位于字符串的最末尾。
这是针对我正在设置的 Yahoo Pipe 的。预先感谢您的帮助。 -麦克风
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正则表达式
\bdog$
仅匹配字符串末尾。如果关键字后面可以有空格,请尝试\bdog\s*$
。如果您希望在dog
之后允许使用其他字符(字母数字除外),例如标点符号,请使用\bdog\W*$
。\b
是一个单词边界锚点,可确保仅匹配整个单词dog
,而不是像underdog
中那样是单词的一部分。\s
匹配空格。\w
匹配字母数字字符;\W
匹配任何非alnum 的内容。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) afterdog
, for example punctuation, then use\bdog\W*$
.\b
is a word boundary anchor that makes sure that only an entire worddog
is matched - not part of a word as inunderdog
.\s
matches whitespace.\w
matches an alphanumeric characters;\W
matches anything that's not an alnum.dog
.
dog
.