From: Robert Klemme Date: 2006-05-26T19:34:33+09:00 Subject: Re: Regexp to esclude substrings 2006/5/25, vincent : > I'm trying to resolve a very simple problem, but I'm encountering some > issues. > > Input: "this is a string where you can find some words" > Output: [this, string, where, can, find, some, words] irb(main):001:0> "this is a string where you can find some words".scan(/\w+/).reject {|w| case w; when "is", "a", "you" then true else false end } => ["this", "string", "where", "can", "find", "some", "words"] > So I'm trying to eliminate ' is ', ' a ' and ' you ' from the string > (note the spaces, 'this' shouldn't be eliminated just because it > includes 'is'). > > I tried the following (and many others), but it doens't work: > my_string = input_string.gsub!(/\s+(is|a|you)\s+/i,' ').split Anchoring at word boundaries helps: irb(main):006:0> "this is a string where you can find some words".gsub(/\b(is|a|you)\s+/, ' ').split /\s+/ => ["this", "string", "where", "can", "find", "some", "words"] This one is probably a bit more efficient irb(main):011:0> require 'enumerator' => true irb(main):012:0> "this is a string where you can find some words".to_enum(:scan, /\w+/).select {|w| /^(is|a|you)$/ !~ w } => ["this", "string", "where", "can", "find", "some", "words"] There's a multitude of other solutions as the number of answers indicates. :-) Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/