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

D

David Charles

#[email protected] 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 (e-mail address removed)
#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 ‘[email protected]’ 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.
 
R

Robert Klemme

#[email protected] 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 (e-mail address removed)
#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 ‘[email protected]’ 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.

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" (e-mail address removed)
irb(main):005:0" baz
irb(main):006:0" STR
=> "foo\nbar\[email protected]\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
 
D

David Charles

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top