On Dec 1, 3:10 am, yermej <yer... / gmail.com> wrote:
> 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.

Not to blow my own horn, but I think the behavior requested by the OP
is still  /.*(cat.*dog)/...("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'").

But don't feel like you have to be 100% correct in order to help out.
My answers are often bass-ackwards (not really a bragging point, heh).
But the point is, like you say, to learn from each other and help
where we can. None of us knows it all; all we can do is our best, and
pray it might help someone here or there. :)

Regards,
Jordan