Forum: Ruby A regexp for an include? method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
057a903d80053e2e243a81af561f0803?d=identicon&s=25 Boucher, Eric (Guest)
on 2006-02-22 13:54
(Received via mailing list)
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.
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-22 14:09
(Received via mailing list)
Boucher, Eric <eric.boucher@messier-dowty.com> 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
12a71a456ac3d464914a8267f11d43b3?d=identicon&s=25 semmons99@gmail.com (Guest)
on 2006-02-22 14:39
(Received via mailing list)
What about using:
 data.match(/TC_TP/)
should return nil if it doesn't match, otherwise returns the matching
string.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-02-22 14:55
(Received via mailing list)
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 Gray II
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-22 18:56
(Received via mailing list)
semmons99@gmail.com 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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-02-22 20:49
(Received via mailing list)
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/)
This topic is locked and can not be replied to.