From: Phrogz Date: 2007-12-14T04:09:59+09:00 Subject: Re: Regexp Matching Not Blank And Not A Specific Word? On Dec 13, 11:16 am, Andrew Stewart wrote: > Hi, > > Please could somebody show me how to write a regular expression that > matches when the input is not blank and it's not a specific word, > e.g. dog. > > For example, I would hope for these results: > > '' =~ regexp # nil > 'dog' =~ regexp # nil > 'cat' =~ regexp # not nil irb(main):006:0> tests = [ "", "dog", "cat", "doggone it" ] => ["", "dog", "cat", "doggone it"] irb(main):007:0> tests.each do |str| irb(main):008:1* hit = (str =~ /^(?!dog).+$|^dog.+$/) irb(main):009:1> puts "#{str.inspect} => #{hit.inspect}" irb(main):010:1> end "" => nil "dog" => nil "cat" => 0 "doggone it" => 0