# Include? won't find match even though it's in String

**URL:** <https://www.ruby-forum.com/t/include-wont-find-match-even-though-its-in-string/171880>\
**Category:** Ruby\
**Created:** [July 10, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/include-wont-find-match-even-though-its-in-string/171880 "2009-07-10T17:11:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 10, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/include-wont-find-match-even-though-its-in-string/171880/1 "2009-07-10T17:11:08Z")

</div>

#removed\_email\_address@domain.invalid is in the string test12 which is a string of email  
addresses #separated by newline characters  
#puts test12 prints out very nice listing of all email addresses on a  
separate #line including the aforementioned removed\_email\_address@domain.invalid  
#I tried it with a test13 object in which I “slice!”(ed) everything out  
#according to /[A-Za-z0-9\_.%@-]+ / but I got the same results and puts  
test13 #basically shows the same as test12 with the newline characters  
removed which #was what I expected.

if(test12.include? “person@coge”) then  
puts “person@coge found”  
else  
puts “person@coge not found”  
end  
------\> Outputs -----\> â€˜person@coge foundâ€™

## if(test12.include? “person@cogec”) then puts “person@cogec found” else puts “person@cogec not found” end -------\>Outputs -------\> â€˜person@cogec not foundâ€™

This is so bizarre I canâ€™t explain it… I even tried splicing out all  
newline characters etc and I still get the same results. As soon as I  
get to the second â€˜câ€™ in cogec the string is not found in test12. Of  
course my goal is to be able to find â€˜removed\_email\_address@domain.invalidâ€™ in the string  
test12 since it is in the string test12.

(please note that Iâ€™ve changed the original email address to person in  
order to protect privacy)

Please let me know if there is a solution to this problem or if you have  
any ideas.

thanks in advance,  
Dave.

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 10, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/include-wont-find-match-even-though-its-in-string/171880/2 "2009-07-10T17:29:55Z")

</div>

Hi –

On Sat, 11 Jul 2009, David Charles wrote:

> puts “person@coge found”  
> -------\>Outputs -------\> â€˜person@cogec not foundâ€™
> 
> Please let me know if there is a solution to this problem or if you have  
> any ideas.

There has to be something going on beyond what you’re describing.  
String#include? does work:

> > str = “removed\_email\_address@domain.invalid”  
> > =\> “removed\_email\_address@domain.invalid”
> 
> > str.include?(“person@coge”)  
> > =\> true
> 
> > str.include?(“person@cogec”)  
> > =\> true

Can you post an entire irb session, or equivalent, where you’re  
getting this problem, including showing the value of the whole string  
(edited for privacy, of course 🙂

David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 10, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/include-wont-find-match-even-though-its-in-string/171880/3 "2009-07-10T19:05:49Z")

</div>

On 10.07.2009 17:11, David Charles wrote:

> ```
> puts "person@coge found"
> 
> ```
> 
> -------\>Outputs -------\> ‘person@cogec not found’
> 
> Please let me know if there is a solution to this problem or if you have  
> any ideas.
> 
> thanks in advance,  
> Dave.

What Ruby version? Can you show the exact code to build the string  
test12? It works for me even on 1.8:

$ irb  
irb(main):001:0\> s=\<\<STR  
irb(main):002:0" foo  
irb(main):003:0" bar  
irb(main):004:0" removed\_email\_address@domain.invalid  
irb(main):005:0" baz  
irb(main):006:0" STR  
=\> “foo\nbar\removed\_email\_address@domain.invalid\nbaz\n”  
irb(main):007:0\> s.include? “person@coge”  
=\> true  
irb(main):008:0\> s.include? “person@cogec”  
=\> true  
irb(main):009:0\>

I suggest to print the string with p to discover control characters you  
might have in there.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 10, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/include-wont-find-match-even-though-its-in-string/171880/4 "2009-07-10T19:30:08Z")

</div>

Hi, thanks for the help guys. I actually just resolved the problem.  
It’s interesting how typing out the problem helped me solve it in this  
case. I guess this should be a lesson to comment my code more  
concisely. 🙂
