# Regexp Matching Not Blank And Not A Specific Word?

**URL:** <https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062>\
**Category:** Ruby\
**Created:** [December 13, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062 "2007-12-13T19:17:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062/1 "2007-12-13T19:17:19Z")

</div>

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

I have tried various permutations and combinations involving (?!re)  
but I just can’t find the answer. In fact I’m beginning to doubt  
that (?!re) works at all on my system (Ruby 1.8.6)…but on balance  
it’s probably me rather than my Ruby installation!

Thanks and regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062/2 "2007-12-13T19:47:51Z")

</div>

Andrew S. wrote:

> In fact I’m beginning to doubt  
> that (?!re) works at all on my system (Ruby 1.8.6)…but on balance  
> it’s probably me rather than my Ruby installation!

It’s not you. The ruby 1.8 regexen don’t support negative lookahead.  
You’ll  
get that in ruby 1.9 or by installing and using the oniguruma  
regexp-engine  
instead of the default ruby 1.8 one.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062/3 "2007-12-13T20:06:21Z")

</div>

On Dec 13, 11:47 am, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Andrew S. wrote:
> 
> > In fact I’m beginning to doubt  
> > that (?!re) works at all on my system (Ruby 1.8.6)…but on balance  
> > it’s probably me rather than my Ruby installation!
> 
> It’s not you. The ruby 1.8 regexen don’t support negative lookahead. You’ll  
> get that in ruby 1.9 or by installing and using the oniguruma regexp-engine  
> instead of the default ruby 1.8 one.

Not true:  
C:\>ruby -v  
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

C:\>irb  
irb(main):001:0\> s = “hello world”  
=\> “hello world”  
irb(main):002:0\> s.scan /[eo]./  
=\> [“el”, "o ", “or”]  
irb(main):003:0\> s.scan /eo./  
=\> [“el”, "o "]

It just doesn’t support negative or positive _lookbehinds_.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062/4 "2007-12-13T21:07:14Z")

</div>

On Dec 13, 2007 11:09 AM, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “doggone it” =\> 0  
> Just to shill for a moment:

I was doing that kind of irb stuff so often for regexps in Ruby, I wrote  
this:

[http://rubyforge.org/projects/regexpbench/](http://rubyforge.org/projects/regexpbench/)  
[http://regexpbench.rubyforge.org/](http://regexpbench.rubyforge.org/)

Hope it’s useful.

Judson

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062/5 "2007-12-13T20:10:29Z")

</div>

On Dec 13, 11:16 am, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ‘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

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 14, 2007, 2:09pm UTC](https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062/6 "2007-12-14T14:09:22Z")

</div>

On 13 Dec 2007, at 20:06, Judson L. wrote:

> Hope it’s useful.  
> That looks very useful. I’m often twiddling regexps (time-  
> consumingly) so I’ll give it a try.

Thanks!

Regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 14, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/regexp-matching-not-blank-and-not-a-specific-word/123062/7 "2007-12-14T14:05:14Z")

</div>

On 13 Dec 2007, at 19:09, Phrogz wrote:

> > ‘dog’ =~ regexp # nil  
> > “” =\> 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 S.
