A regexp for an include? method

B

Boucher, Eric

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.
 
R

Robert Klemme

Boucher said:
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.

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
 
S

semmons99

What about using:
data.match(/TC_TP/)
should return nil if it doesn't match, otherwise returns the matching
string.
 
R

Robert Klemme

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
 
W

William James

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.

myArray.flatten.grep(/myPattern/)
 

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

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,171
Latest member
EllaHolmwo

Latest Threads

Top