Phrogz
12/13/2007 7:07:00 PM
On Dec 13, 11:16 am, Andrew Stewart <b...@airbladesoftware.com> 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