On Thu, Jun 7, 2012 at 1:08 AM, cyber c. <lists / ruby-forum.com> wrote:
> Hi,
>
> I dont understand why this doesnt work
>
> Valid: aecd, efcd, mncd
> Invalid: if ab comes before cd like abcd
>
>
> Reg exp : /(?!ab)cd/
>
> But this doesn't work. I looked at this tutorial
> http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm which
> says something of this kind should work.


(?!) is the look-ahead. It's used to match something followed (or not)
by something else. You could use this if you wanted to match "ab" that
is not followed by "cd".
But, you want look-behind, because you want to match a "cd" that is
not preceded by "ab". So you need:

1.9.2p290 :003 > "abcd".match(/(?<!ab)cd/)
 => nil
1.9.2p290 :004 > "aecd".match(/(?<!ab)cd/)
 => #<MatchData "cd">

Jesus.