Using Regular Expressions in Arrays

Discussion in 'Ruby' started by JT Kimbell, Jan 23, 2007.

  1. JT Kimbell

    JT Kimbell Guest

    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/.
     
    JT Kimbell, Jan 23, 2007
    #1
    1. Advertising

  2. JT Kimbell

    Tim Hunter Guest

    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.
     
    Tim Hunter, Jan 23, 2007
    #2
    1. Advertising

  3. 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 Biedenharn, Jan 23, 2007
    #3
  4. 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
     
    Stefano Crocco, Jan 23, 2007
    #4
  5. On 1/23/07, JT Kimbell <> 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
     
    Luis Parravicini, Jan 23, 2007
    #5
  6. JT Kimbell

    JT Kimbell Guest

    JT Kimbell, Jan 23, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    609
    Jay Douglas
    Aug 15, 2003
  2. Guest
    Replies:
    1
    Views:
    376
    Jerry III
    Oct 19, 2003
  3. Chris Leffer

    Validating paths using regular expressions

    Chris Leffer, Feb 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    303
    Chris Leffer
    Feb 21, 2005
  4. Philipp
    Replies:
    21
    Views:
    1,130
    Philipp
    Jan 20, 2009
  5. Noman Shapiro
    Replies:
    0
    Views:
    234
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page