# Regexp exclusion search - find matches NOT ending with a string?

**URL:** <https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448>\
**Category:** ruby-talk\
**Created:** [17 July 2009 00:35 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448 "2009-07-17T00:35:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrendanC](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@BrendanC](https://rubytalk.org/u/BrendanC)\
**Post date:** [17 July 2009 00:35 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/1 "2009-07-17T00:35:33Z")

</div>

I have the following text in a file:

1 a1.html  
2 b.doc  
3 c.xml  
4 d.tiff  
5 e.jpeg  
6 f.html  
....

I need a regexp to match lines except those that end with ending in  
".html" - iow - I want lines 2-5 above. I believe this may require a  
negative lookbehind match. I tried the following but Ruby (1.8) gives  
an undefined sequence error :

$(?\<!\.html) # \<---- this seems to work with other engines

Before you jump re Ruby the version I also tested this here -  
[http://www.rubyxp.com/](http://www.rubyxp.com/) and get invalid expression (fyi this tests with  
Ruby 1.9). Any ideas/alternatives?

TIA,  
BC

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [17 July 2009 00:58 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/2 "2009-07-17T00:58:37Z")

</div>

The easiest path is to negate that it matches, say for instance:

&nbsp;&nbsp;&nbsp;if filename !~ /\.html\z/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# non-HTML here  
&nbsp;&nbsp;&nbsp;end

-- fxn

> **···**
>
> On Fri, Jul 17, 2009 at 2:35 AM, BrendanC\<brencam@gmail.com\> wrote:
> 
> > I have the following text in a file:
> > 
> > 1 a1.html  
> > 2 b.doc  
> > 3 c.xml  
> > 4 d.tiff  
> > 5 e.jpeg  
> > 6 f.html  
> > ....
> > 
> > I need a regexp to match lines except those that end with ending in  
> > ".html"

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [17 July 2009 00:59 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/3 "2009-07-17T00:59:53Z")

</div>

Hi --

> **···**
>
> On Fri, 17 Jul 2009, BrendanC wrote:
> 
> > I have the following text in a file:
> > 
> > 1 a1.html  
> > 2 b.doc  
> > 3 c.xml  
> > 4 d.tiff  
> > 5 e.jpeg  
> > 6 f.html  
> > ....
> > 
> > I need a regexp to match lines except those that end with ending in  
> > ".html" - iow - I want lines 2-5 above. I believe this may require a  
> > negative lookbehind match. I tried the following but Ruby (1.8) gives  
> > an undefined sequence error :
> > 
> > $(?\<!\.html) # \<---- this seems to work with other engines
> > 
> > Before you jump re Ruby the version I also tested this here -  
> > [http://www.rubyxp.com/](http://www.rubyxp.com/) and get invalid expression (fyi this tests with  
> > Ruby 1.9). Any ideas/alternatives?
> 
> I would probably do:
> 
> &nbsp;&nbsp;&nbsp;lines.reject {|line| line =~ /html$/ }
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Now available: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))  
> Training! Intro to Ruby, with Black & Kastner, September 14-17  
> (More info: [http://rubyurl.com/vmzN\](http://rubyurl.com/vmzN%5C))

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [17 July 2009 08:54 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/4 "2009-07-17T08:54:34Z")

</div>

Xavier and David gave good advice.  
If however you really have to have a matching regex

&nbsp;&nbsp;%r($(?\<!\.html)\z) # is that what you meant above?

works fine. I believe that you can install Oniguruma on 1.8 as a gem  
for that purpose.  
HTH  
Robert

> **···**
>
> On 7/17/09, BrendanC \<brencam@gmail.com\> wrote:
> 
> > I have the following text in a file:
> > 
> > 1 a1.html  
> > 2 b.doc  
> > 3 c.xml  
> > 4 d.tiff  
> > 5 e.jpeg  
> > 6 f.html  
> > ....
> > 
> > I need a regexp to match lines except those that end with ending in  
> > ".html" - iow - I want lines 2-5 above. I believe this may require a  
> > negative lookbehind match. I tried the following but Ruby (1.8) gives  
> > an undefined sequence error :
> > 
> > $(?\<!\.html) # \<---- this seems to work with other engines
> > 
> > Before you jump re Ruby the version I also tested this here -  
> > [http://www.rubyxp.com/](http://www.rubyxp.com/) and get invalid expression (fyi this tests with  
> > Ruby 1.9). Any ideas/alternatives?
> 
> --  
> Toutes les grandes personnes ont d’abord été des enfants, mais peu  
> d’entre elles s’en souviennent.
> 
> All adults have been children first, but not many remember.
> 
> [Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [18 July 2009 05:45 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/5 "2009-07-18T05:45:41Z")

</div>

BrendanC wrote:

> I have the following text in a file:
> 
> 1 a1.html  
> 2 b.doc  
> 3 c.xml  
> 4 d.tiff  
> 5 e.jpeg  
> 6 f.html  
> ....
> 
> I need a regexp to match lines except those that end with ending in  
> ".html" - iow - I want lines 2-5 above.

Some alternate means to the same end:

IO.foreach("data.txt") do |line|

&nbsp;&nbsp;#1  
&nbsp;&nbsp;if line.chomp.split(".")[-1] != "html"  
&nbsp;&nbsp;&nbsp;&nbsp;puts line  
&nbsp;&nbsp;end

&nbsp;&nbsp;#2  
&nbsp;&nbsp;if line[-5, 4] != "html"  
&nbsp;&nbsp;&nbsp;&nbsp;print line  
&nbsp;&nbsp;end

&nbsp;&nbsp;#3  
&nbsp;&nbsp;if line.slice(-5..-1) != "html"  
&nbsp;&nbsp;&nbsp;&nbsp;print line  
&nbsp;&nbsp;end

&nbsp;&nbsp;puts  
end

--output:--  
2 b.doc  
2 b.doc  
2 b.doc

3 c.xml  
3 c.xml  
3 c.xml

4 d.tiff  
4 d.tiff  
4 d.tiff

5 e.jpeg  
5 e.jpeg  
5 e.jpeg

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [17 July 2009 08:56 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/6 "2009-07-17T08:56:49Z")

</div>

> &nbsp;&nbsp;%r($(?\<!\.html)\z) # is that what you meant above?

where does this $ come from ?

---

<div class="post-metadata">

**Author:** ![Glenn\_Jackman](https://avatars.discourse-cdn.com/v4/letter/g/ce73a5/32.png) [@Glenn\_Jackman](https://rubytalk.org/u/Glenn_Jackman)\
**Post date:** [17 July 2009 15:30 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/7 "2009-07-17T15:30:10Z")

</div>

Is the Ruby regular expression syntax documented anywhere?

I was attempting to use a look-behind, but it's not supported.

The syntax is not documented in the RegExp rdocs, and I haven't seen a  
site that spells out all the nitty-gritty details and pokes into the  
dark corners.

I'm looking for the Ruby equivalent of:  
&nbsp;&nbsp;&nbsp;&nbsp;[re\_syntax manual page - Tcl Built-In Commands](http://www.tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm)  
&nbsp;&nbsp;&nbsp;&nbsp;[re — Regular expression operations — Python 3.12.1 documentation](http://docs.python.org/library/re.html#regular-expression-syntax)  
&nbsp;&nbsp;&nbsp;&nbsp;[perlre - Perl regular expressions - Perldoc Browser](http://perldoc.perl.org/perlre.html)

Does it exist?

> **···**
>
> At 2009-07-16 08:59PM, "David A. Black" wrote:
> 
> > On Fri, 17 Jul 2009, BrendanC wrote:  
> > \> $(?\<!\.html) # \<---- this seems to work with other engines
> > 
> > I would probably do:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;lines.reject {|line| line =~ /html$/ }
> 
> --  
> Glenn Jackman  
> &nbsp;&nbsp;&nbsp;&nbsp;Write a wise saying and your name will live forever. -- Anonymous

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [17 July 2009 16:15 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/8 "2009-07-17T16:15:24Z")

</div>

You could try the Regular Expressions section of the Standard Types chapter of Programming Ruby. Be advised that this is the online version of the 1st edition that is now 8 years old. Since you seem to be using a version 1.8.x of Ruby, the Regexp parts are going to be mostly the same.

[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Jul 17, 2009, at 11:30 AM, Glenn Jackman wrote:
> 
> > At 2009-07-16 08:59PM, "David A. Black" wrote:
> > 
> > > On Fri, 17 Jul 2009, BrendanC wrote:
> > > 
> > > > $(?\<!\.html) # \<---- this seems to work with other engines
> > > 
> > > I would probably do:
> > > 
> > > &nbsp;&nbsp;&nbsp;lines.reject {|line| line =~ /html$/ }
> > 
> > Is the Ruby regular expression syntax documented anywhere?
> > 
> > I was attempting to use a look-behind, but it's not supported.
> > 
> > The syntax is not documented in the RegExp rdocs, and I haven't seen a  
> > site that spells out all the nitty-gritty details and pokes into the  
> > dark corners.
> > 
> > I'm looking for the Ruby equivalent of:  
> > &nbsp;&nbsp;&nbsp;[re\_syntax manual page - Tcl Built-In Commands](http://www.tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm)  
> > &nbsp;&nbsp;&nbsp;[re — Regular expression operations — Python 3.12.1 documentation](http://docs.python.org/library/re.html#regular-expression-syntax)  
> > &nbsp;&nbsp;&nbsp;[perlre - Perl regular expressions - Perldoc Browser](http://perldoc.perl.org/perlre.html)
> > 
> > Does it exist?
> > 
> > -- Glenn Jackman  
> > &nbsp;&nbsp;&nbsp;Write a wise saying and your name will live forever. -- Anonymous

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [17 July 2009 19:28 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/9 "2009-07-17T19:28:29Z")

</div>

> > \> $(?\<!\.html) # \<---- this seems to work with other engines
> > 
> > I would probably do:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;lines.reject {|line| line =~ /html$/ }
> 
> Is the Ruby regular expression syntax documented anywhere?
> 
> I was attempting to use a look-behind, but it's not supported.
> 
> The syntax is not documented in the RegExp rdocs, and I haven't seen a  
> site that spells out all the nitty-gritty details and pokes into the  
> dark corners.
> 
> I'm looking for the Ruby equivalent of:  
> &nbsp;&nbsp;&nbsp;&nbsp;[re\_syntax manual page - Tcl Built-In Commands](http://www.tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm)  
> &nbsp;&nbsp;&nbsp;&nbsp;[re — Regular expression operations — Python 3.12.1 documentation](http://docs.python.org/library/re.html#regular-expression-syntax)  
> &nbsp;&nbsp;&nbsp;&nbsp;[perlre - Perl regular expressions - Perldoc Browser](http://perldoc.perl.org/perlre.html)
> 
> Does it exist?

For Oniguruma I found this most helpful  
[http://manual.macromates.com/en/regular\_expressions#regular\_expressions](http://manual.macromates.com/en/regular_expressions#regular_expressions)

> --  
> Glenn Jackman  
> &nbsp;&nbsp;&nbsp;&nbsp;Write a wise saying and your name will live forever. -- Anonymous

Nice one

Cheers  
Robert

> **···**
>
> On 7/17/09, Glenn Jackman \<glennj@ncf.ca\> wrote:
> 
> > At 2009-07-16 08:59PM, "David A. Black" wrote:
> > 
> > > On Fri, 17 Jul 2009, BrendanC wrote:

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [18 July 2009 19:34 UTC](https://rubytalk.org/t/regexp-exclusion-search-find-matches-not-ending-with-a-string/54448/10 "2009-07-18T19:34:59Z")

</div>

Glenn Jackman wrote:

> Is the Ruby regular expression syntax documented anywhere?
> 
> I was attempting to use a look-behind, but it's not supported.
> 
> The syntax is not documented in the RegExp rdocs

In my opinion, documentation is Ruby's weakest aspect by far - and the  
deficiency has gotten substantially worse with ruby 1.9.

Best available information is in third-party books, which presumably  
have reverse-engineered from the source code. I fairly often resort to  
irb to check behaviour is what I want, and have on occasions had to  
resort to reading the source.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
