BlogNotesAbout
moon indicating dark mode
sun indicating light mode

Negative lookaheads in ruby

A negative look ahead is simply saying “the next character(s) cannot be <insert character(s) here>“. The difference between (?!o) and [^o] is (?!o) does not match the character(s) in question, while [^o] does.

[1] pry(main)> /Brand(?!o)n/ =~ 'Brandn'
0
[2] pry(main)> /Brand[^o]n/ =~ 'Brandn'
nil
[3] pry(main)> /Brand[^o]n/ =~ 'Brandin'
0

Since [^o] matched n in the second example, we can’t match against it ourselves.