On Dec 1, 2:19 am, Raul Parolari <raulparol... / gmail.com> wrote: > yermej wrote: > > > Nonmatching (or negative) lookahead is what you want, and with some > > adjustment of the capture you get: > > > 'cat sheep horse cat tac dog' =~ /(cat(?!.*cat).*dog)/ > > => 16 > > $1 > > => "cat tac dog" > > Negative lookaheads that contain '.*' are hard to comprehend (at least > for me). It is enough to add a 'cat' at the end and the regexp does not > find any more the 'cat tac dog' that should be matched: > > 'cat sheep horse cat tac dog lion cat' =~ /(cat(?!.*cat).*dog)/ > => nil > > Also notice that, when it works, it will always give the last expression > present: > 'cat sheep horse cat tac dog lion cat dog' =~ /(cat(?!.*cat).*dog)/ > => 33 > p $1 # => "cat dog" > > Daniel Sheppard wrote: > > Working out negative regular expressions is normally best avoided. > > I would say that it is true when they contain '.*' type expressions; > else they can be extremely useful. > > > 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"] > > Very ingenious... > > Raul > -- > Posted viahttp://www.ruby-forum.com/. Thanks, Raul, for the clarification on that. To the original poster, I apologize for the misinformation. I guess when I'm not completely sure about such things, I should start a new thread, but attempting to answer questions here (some of which I do get right) has been a great help to me in my own learning. I think that starting new threads on all occasions would get to be too much and I probably wouldn't get many responses. In the future, I may just keep quiet until I'm either sure I have the correct answer or at least don't understand why my answer isn't correct.