# Regexp to esclude substrings

**URL:** <https://www.ruby-forum.com/t/regexp-to-esclude-substrings/61583>\
**Category:** Ruby\
**Created:** [May 25, 2006, 7:24am UTC](https://www.ruby-forum.com/t/regexp-to-esclude-substrings/61583 "2006-05-25T07:24:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincent](https://www.ruby-forum.com/u/vincent)\
**Post date:** [May 25, 2006, 7:24am UTC](https://www.ruby-forum.com/t/regexp-to-esclude-substrings/61583/1 "2006-05-25T07:24:09Z")

</div>

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]

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

Any hints?

---

<div class="post-metadata">

**Author:** ![vincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincent](https://www.ruby-forum.com/u/vincent)\
**Post date:** [May 25, 2006, 7:42am UTC](https://www.ruby-forum.com/t/regexp-to-esclude-substrings/61583/2 "2006-05-25T07:42:00Z")

</div>

On 25-May-06, at 1:24 AM, vincent wrote:

> I tried the following (and many others), but it doens’t work:  
> my\_string = input\_string.gsub!(/\s+(is|a|you)\s+/i,

my\_string = input\_string.gsub!(/\s+\b(is|a|you)\b\s+/i, ’ ').split

–  
Jeremy T.  
removed\_email\_address@domain.invalid

“The proof is the proof that the proof has been proven and that’s the  
proof!” - Jean ChrÃ©tien

---

<div class="post-metadata">

**Author:** ![vincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincent](https://www.ruby-forum.com/u/vincent)\
**Post date:** [May 25, 2006, 7:57am UTC](https://www.ruby-forum.com/t/regexp-to-esclude-substrings/61583/3 "2006-05-25T07:57:30Z")

</div>

vincent [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I tried the following (and many others), but it doens’t work:  
> my\_string = input\_string.gsub!(/\s+(is|a|you)\s+/i,’ ').split
> 
> Any hints?

Well, since you want an array of the words back, why don’t you do  
something like

my\_string.split.reject { |word| word =~/^(is|a|you)$/ }  
or  
my\_string.split.reject { |word| %w(is a you).include? word }

Seems simpler to me than to replace the words in the string and then  
split it into an array.

Tom

---

<div class="post-metadata">

**Author:** ![vincent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincent](https://www.ruby-forum.com/u/vincent)\
**Post date:** [May 26, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/regexp-to-esclude-substrings/61583/4 "2006-05-26T12:36:59Z")

</div>

2006/5/25, vincent [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
