From: Andrew Stewart Date: 2007-12-14T21:58:46+09:00 Subject: Re: Regexp Matching Not Blank And Not A Specific Word? On 13 Dec 2007, at 19:09, Phrogz wrote: > 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 That's fantastic, thank you! When I was trying this yesterday, I think I failed to appreciate that (?!re) doesn't consume the match. Now I know. Thanks also for the tip in your previous email about Ruby 1.8 and lookbehinds. Regards, Andy Stewart ------- http://airbladesoftware.com