# How add "Not-match" logic into Enumerable.grep?

**URL:** <https://www.ruby-forum.com/t/how-add-not-match-logic-into-enumerable-grep/105992>\
**Category:** Ruby\
**Created:** [June 5, 2007, 11:56am UTC](https://www.ruby-forum.com/t/how-add-not-match-logic-into-enumerable-grep/105992 "2007-06-05T11:56:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 11:56am UTC](https://www.ruby-forum.com/t/how-add-not-match-logic-into-enumerable-grep/105992/1 "2007-06-05T11:56:02Z")

</div>

Say, I want to get all files in one folder. Since Dir.entries will  
have “.” and “…” included. I want to strip them out with “grep”. I  
tried to write in such a way as I can did similarly in Perl:  
log\_files = Dir.entries(log\_dir).grep(!/^…?/)  
But, it is a syntax error to have “!/regex/” as grep’s argument.

Should I use some trick to make grep accept “not-match” regex?

---

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 1:02pm UTC](https://www.ruby-forum.com/t/how-add-not-match-logic-into-enumerable-grep/105992/2 "2007-06-05T13:02:44Z")

</div>

Hi,

At Tue, 5 Jun 2007 18:55:02 +0900,  
Morgan C. wrote in [ruby-talk:254395]:

> Say, I want to get all files in one folder. Since Dir.entries will  
> have “.” and “…” included. I want to strip them out with “grep”. I  
> tried to write in such a way as I can did similarly in Perl:  
> log\_files = Dir.entries(log\_dir).grep(!/^…?/)

grep(/\A(?!..?\z)/)

---

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/how-add-not-match-logic-into-enumerable-grep/105992/3 "2007-06-05T13:10:46Z")

</div>

On 05.06.2007 11:54, Morgan C. wrote:

> Say, I want to get all files in one folder. Since Dir.entries will  
> have “.” and “…” included. I want to strip them out with “grep”. I  
> tried to write in such a way as I can did similarly in Perl:  
> log\_files = Dir.entries(log\_dir).grep(!/^…?/)  
> But, it is a syntax error to have “!/regex/” as grep’s argument.
> 
> Should I use some trick to make grep accept “not-match” regex?

Use #reject:

Dir.entries(".").reject {|x| /\A.+\z/ =~ x}

robert
