From: Rob Biedenharn Date: 2007-01-24T02:41:05+09:00 Subject: Re: Using Regular Expressions in Arrays On Jan 23, 2007, at 12:29 PM, JT Kimbell wrote: > Hey everyone, > > Right now I have an array of terms and I want to delete/find a term > using pattern matching. > > For example, I have the following array (this is a very simple > example). > > x = ["dog", "cat", "moose"] > > x.include?(/dog/) returns false. > > Now, I understand regular expressions, and can usually get one that > works, but perhaps I am using them wrong in ruby? > > Anyway, my question is if I can use regex to find an array element(s)? > > Thanks, > > JT Take a look at Array#grep (which is really Enumerable#grep) >> x = ["dog", "cat", "moose"] => ["dog", "cat", "moose"] >> x.grep(/dog/) => ["dog"] >> x << "frog" => ["dog", "cat", "moose", "frog"] >> x.grep(/og/) => ["dog", "frog"] -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com