[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A regexp for an include? method

Boucher, Eric

2/22/2006 12:52:00 PM

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.



4 Answers

Robert Klemme

2/22/2006 1:04:00 PM

0

Boucher, Eric <eric.boucher@messier-dowty.com> wrote:
> 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

Shane Emmons

2/22/2006 1:35:00 PM

0

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

Robert Klemme

2/22/2006 5:53:00 PM

0

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

William James

2/22/2006 7:44:00 PM

0

Boucher, Eric wrote:
> 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/)