# A regexp for an include? method

**URL:** <https://www.ruby-forum.com/t/a-regexp-for-an-include-method/71804>\
**Category:** Ruby\
**Created:** [February 22, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/a-regexp-for-an-include-method/71804 "2006-02-22T13:54:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BoucherS\_Eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@BoucherS\_Eric](https://www.ruby-forum.com/u/BoucherS_Eric)\
**Post date:** [February 22, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/a-regexp-for-an-include-method/71804/1 "2006-02-22T13:54:33Z")

</div>

Hi,

I want to see if a certain pattern is inside an array. I tried:  
myArray.flatten.include?(/myPattern/)

but it always return false. Why? Is it because the “include?” doesn’t  
takes  
regular expressions?  
Here’s another example that I tried inside an irb session:

data = [“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]  
=\>[“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]  
data.include?(/TC\_TP/)  
=\> false  
data.include?(/TC\_TP.\*/)  
=\> false  
data.include?(/TC\_TP1/)  
=\> false  
data.include?(“TC\_TP1”)  
=\> true !!!

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![BoucherS\_Eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@BoucherS\_Eric](https://www.ruby-forum.com/u/BoucherS_Eric)\
**Post date:** [February 22, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/a-regexp-for-an-include-method/71804/2 "2006-02-22T14:09:49Z")

</div>

Boucher, Eric [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\>[“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]  
> data.include?(/TC\_TP/)  
> =\> false  
> data.include?(/TC\_TP.\*/)  
> =\> false  
> data.include?(/TC\_TP1/)  
> =\> false  
> data.include?(“TC\_TP1”)  
> =\> true !!!
> 
> Thanks for your help.

It’s because include? uses #eql? or == for comparison while regexp  
matching  
is done via === and =~. You can use #find, #select or #any?

irb(main):042:0\> data.find {|x| /TC\_TP/ =~ x}  
=\> “TC\_TP1”  
irb(main):043:0\> data.find {|x| /XTC\_TP/ =~ x}  
=\> nil  
irb(main):044:0\> data.select {|x| /XTC\_TP/ =~ x}  
=\> []  
irb(main):045:0\> data.select {|x| /TC\_TP/ =~ x}  
=\> [“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]  
irb(main):046:0\> data.any? {|x| /TC\_TP/ =~ x}  
=\> true  
irb(main):047:0\> data.any? {|x| /YTC\_TP/ =~ x}  
=\> false

I’d go for any? as it short circuits, i.e. terminates after the first  
successful match.

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![BoucherS\_Eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@BoucherS\_Eric](https://www.ruby-forum.com/u/BoucherS_Eric)\
**Post date:** [February 22, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/a-regexp-for-an-include-method/71804/3 "2006-02-22T14:39:38Z")

</div>

What about using:  
data.match(/TC\_TP/)  
should return nil if it doesn’t match, otherwise returns the matching  
string.

---

<div class="post-metadata">

**Author:** ![BoucherS\_Eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@BoucherS\_Eric](https://www.ruby-forum.com/u/BoucherS_Eric)\
**Post date:** [February 22, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/a-regexp-for-an-include-method/71804/4 "2006-02-22T14:55:00Z")

</div>

On Feb 22, 2006, at 6:52 AM, Boucher, Eric wrote:

> data = [“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]  
> =\>[“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]  
> data.include?(/TC\_TP/)  
> =\> false  
> data.include?(/TC\_TP.\*/)  
> =\> false  
> data.include?(/TC\_TP1/)  
> =\> false  
> data.include?(“TC\_TP1”)  
> =\> true !!!

You can use grep() for this:

> > data = [“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]  
> > =\> [“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]
> 
> > data.grep(/TC\_TP/)  
> > =\> [“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]
> 
> > !data.grep(/TC\_TP/).empty?  
> > =\> true

Hope that gives you some fresh ideas.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![BoucherS\_Eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@BoucherS\_Eric](https://www.ruby-forum.com/u/BoucherS_Eric)\
**Post date:** [February 22, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/a-regexp-for-an-include-method/71804/5 "2006-02-22T18:56:20Z")

</div>

removed\_email\_address@domain.invalid wrote:

> What about using:  
> data.match(/TC\_TP/)  
> should return nil if it doesn’t match, otherwise returns the matching  
> string.

It doesn’t return the matching string but an object of type MatchData.  
The  
matched string can be extracted from that. Another form is to use  
String#[]  
with regexp, like

arr.any? {|s| s[/TC\_TP/]}

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![BoucherS\_Eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dc4da7/32.png) [@BoucherS\_Eric](https://www.ruby-forum.com/u/BoucherS_Eric)\
**Post date:** [February 22, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/a-regexp-for-an-include-method/71804/6 "2006-02-22T20:49:01Z")

</div>

Boucher, Eric wrote:

> =\>[“TC\_TP1”, “TC\_TP2”, “TC\_TP3”]  
> data.include?(/TC\_TP/)  
> =\> false  
> data.include?(/TC\_TP.\*/)  
> =\> false  
> data.include?(/TC\_TP1/)  
> =\> false  
> data.include?(“TC\_TP1”)  
> =\> true !!!
> 
> Thanks for your help.

myArray.flatten.grep(/myPattern/)
