# Regexp Matching Not Blank And Not A Specific Word?

**URL:** <https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992>\
**Category:** ruby-talk\
**Created:** [13 December 2007 18:16 UTC](https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992 "2007-12-13T18:16:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)\
**Post date:** [13 December 2007 18:16 UTC](https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992/1 "2007-12-13T18:16:00Z")

</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 Stewart

> **···**
>
> -------  
> [http://airbladesoftware.com](http://airbladesoftware.com)

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [13 December 2007 18:47 UTC](https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992/2 "2007-12-13T18:47:02Z")

</div>

Andrew Stewart 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

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [13 December 2007 19:09 UTC](https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992/3 "2007-12-13T19:09:59Z")

</div>

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

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [13 December 2007 19:05 UTC](https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992/4 "2007-12-13T19:05:01Z")

</div>

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](?!r)./  
=\> ["el", "o "]

It just doesn't support negative or positive \*lookbehinds\*.

> **···**
>
> On Dec 13, 11:47 am, Sebastian Hungerecker \<sep...@googlemail.com\> wrote:
> 
> > Andrew Stewart 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.

---

<div class="post-metadata">

**Author:** ![Judson\_Lester1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/judson_lester1/32/1968_2.png) [@Judson\_Lester1](https://rubytalk.org/u/Judson_Lester1)\
**Post date:** [13 December 2007 20:06 UTC](https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992/5 "2007-12-13T20:06:47Z")

</div>

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

> **···**
>
> On Dec 13, 2007 11:09 AM, Phrogz \<phrogz@mac.com\> wrote:
> 
> > 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
> 
> --  
> Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a  
> grue.

---

<div class="post-metadata">

**Author:** ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)\
**Post date:** [14 December 2007 12:58 UTC](https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992/6 "2007-12-14T12:58:46Z")

</div>

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

> **···**
>
> On 13 Dec 2007, at 19:09, Phrogz wrote:
> 
> > 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
> 
> -------
> 
> > **[AirBlade Software](https://airbladesoftware.com/)**
> >
> > Andrew Stewart of AirBlade Software.

---

<div class="post-metadata">

**Author:** ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)\
**Post date:** [14 December 2007 13:00 UTC](https://rubytalk.org/t/regexp-matching-not-blank-and-not-a-specific-word/42992/7 "2007-12-14T13:00:01Z")

</div>

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

Thanks!

Regards,  
Andy Stewart

> **···**
>
> On 13 Dec 2007, at 20:06, Judson Lester wrote:
> 
> > 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.
> 
> -------
> 
> > **[AirBlade Software](https://airbladesoftware.com/)**
> >
> > Andrew Stewart of AirBlade Software.
