Using Regular Expressions in Arrays

J

JT Kimbell

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
 
T

Tim Hunter

JT said:
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.
 
R

Rob Biedenharn

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
(e-mail address removed)
 
S

Stefano Crocco

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
 
L

Luis Parravicini

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
 

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
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top