Using Regular Expressions in Arrays

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

On Jan 23, 2007, at 12:29 PM, JT Kimbell wrote:

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 B. http://agileconsultingllc.com
[email protected]

Alle 18:29, martedì 23 gennaio 2007, JT Kimbell ha scritto:

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
expression are of class Regexp, not of class String. If you look at the
documentation for the Regexp class, you’ll see that the operator ==
(used by
include?) for Regexp always returns false when the right side is not a
regexp. In other words, the operator == is unrelated to regexp matching.
To
do what you want, you should can use the grep or the any? method in the
Enumerable module.

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

x.grep /dog/
=>[‘dog’]

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

x.grep(/dog/).empty?
This returns true if there’s no match and false if there’s at least one
match

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

Stefano

On 1/23/07, JT Kimbell [email protected] 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

JT Kimbell wrote:

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.

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

JT

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs