Using Regular Expressions in Arrays

  • Follow


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

-- 
Posted via http://www.ruby-forum.com/.

0
Reply jtkimbell (4) 1/23/2007 5:29:47 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
> 

x.include? tests to see if the regular expression /dog/ is an element of 
x, not if some element in x matches /dog/. See the difference?

Anyway, check out Enumerable#find and Array#delete_if.
0
Reply sastph (58) 1/23/2007 5:40:15 PM


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



0
Reply Rob7461 (595) 1/23/2007 5:41:05 PM

Alle 18:29, marted=C3=AC 23 gennaio 2007, JT Kimbell ha scritto:
> 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 =3D ["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

Your code doesn't work because the array contains Strings, and regular=20
expression are of class Regexp, not of class String. If you look at the=20
documentation for the Regexp class, you'll see that the operator =3D=3D (us=
ed by=20
include?) for Regexp always returns false when the right side is not a=20
regexp. In other words, the operator =3D=3D is unrelated to regexp matching=
=2E To=20
do what you want, you should can use the grep or the any? method in the=20
Enumerable module.

* grep returns an array with the elements which match (using the operator =
=3D=3D=3D=20
this time) the given pattern. In your case:

x.grep /dog/
=3D>['dog']

You should then use the empty? on the returned array to see whether there's=
 a=20
match:

x.grep(/dog/).empty?=20
This returns true if there's no match and false if there's at least one mat=
ch


* any? passes each element of the array to the supplied block and returns t=
rue=20
if the block returns true for at least one element:
x.any?{|string| string.match /dog/}

Stefano

0
Reply stefano.crocco (614) 1/23/2007 5:43:01 PM

On 1/23/07, JT Kimbell <jtkimbell@yahoo.com> wrote:
> 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.

Array.include? checks if the array contains the object you passed as argument.

> Anyway, my question is if I can use regex to find an array element(s)?

I'm sure there are better ways of doing it, but as far as I know, I
would do it with using something like:

x.map { |x| x if x =~ /regexp/ }.compact

The array returned are the elements which match the regexp


Luis

0
Reply lparravi (56) 1/23/2007 5:50:44 PM

Thanks everyone, that makes sense and I'll try those out.

JT

-- 
Posted via http://www.ruby-forum.com/.

0
Reply jtkimbell (4) 1/23/2007 6:25:09 PM

5 Replies
44 Views

(page loaded in 0.104 seconds)

Similiar Articles:













7/23/2012 2:42:29 AM


Reply: