From: Daniel Sheppard Date: 2007-11-30T10:24:05+09:00 Subject: Re: Negate a character sequence in a regular expression? > For the following string: > > 'cat sheep horse cat tac dog' > > I would like to write a regular expression that matches any substring > that is prefixed by the word 'cat', is then followed by any characters > as long as those characters do not comprise the word 'cat', and then > finally suffixed by the string 'dog'. Therefore, this expression > should match the substring 'cat tac dog' in the above string. Working out negative regular expressions is normally best avoided. One step is hard. Two steps is not: x = 'cat sheep horse cat tac dog' /(cat.*?dog)/.match(x) && $1.sub(/.*cat/,'cat') Or if you want multiple matches: x = 'cat sheep horse cat tac dog cat cat sheep dog' x.scan(/cat.*?dog/).map {|x| x.sub(/.*cat/,'cat')} => ["cat tac dog", "cat sheep dog"] Dan.