YAENQ: Variable as a pattern

Discussion in 'Ruby' started by basi, Aug 16, 2005.

  1. basi

    basi Guest

    (I ordered for a copy of Mastering Regular Expressions. Should be in my
    snail mailbox anytime soon. In the meantime ... )

    How do I tell Ruby to use the content of a variable as the pattern,
    rather than interpreting the variable name as a literal?


    v = "aeiou"

    Then, in

    "goodness" =~ /[v]/

    I'd like v to be interepreted not as a literal but as a variable name.

    Thanks
    basi
     
    basi, Aug 16, 2005
    #1
    1. Advertising

  2. String interpolation seems to work on regular expressions so try:
    "goodness" =~ /[#{v}]/

    Or more verbosely:
    "goodness" =~ Regexp.new("[#{v}]")

    enjoy,
    .adam
     
    Adam Sanderson, Aug 16, 2005
    #2
    1. Advertising

  3. basi wrote:
    > (I ordered for a copy of Mastering Regular Expressions. Should be in my
    > snail mailbox anytime soon. In the meantime ... )
    >
    > How do I tell Ruby to use the content of a variable as the pattern,
    > rather than interpreting the variable name as a literal?
    >
    >
    > v = "aeiou"
    >
    > Then, in
    >
    > "goodness" =~ /[v]/
    >
    > I'd like v to be interepreted not as a literal but as a variable name.


    Same as with a string:

    "goodness" =~ /[#{v}]/

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Aug 16, 2005
    #3
  4. basi wrote:
    > (I ordered for a copy of Mastering Regular Expressions. Should be in my
    > snail mailbox anytime soon. In the meantime ... )
    >
    > How do I tell Ruby to use the content of a variable as the pattern,
    > rather than interpreting the variable name as a literal?
    >
    >
    > v = "aeiou"
    >
    > Then, in
    >
    > "goodness" =~ /[v]/
    >
    > I'd like v to be interepreted not as a literal but as a variable name.
    >
    > Thanks
    > basi
    >


    "goodness" =~ /#{v}/
     
    Timothy Hunter, Aug 16, 2005
    #4
  5. basi

    basi Guest

    thanks to all who replied. so simple. (goodness, why didn't I think of
    that. sigh.)

    basi


    Timothy Hunter wrote:
    > basi wrote:
    > > (I ordered for a copy of Mastering Regular Expressions. Should be in my
    > > snail mailbox anytime soon. In the meantime ... )
    > >
    > > How do I tell Ruby to use the content of a variable as the pattern,
    > > rather than interpreting the variable name as a literal?
    > >
    > >
    > > v = "aeiou"
    > >
    > > Then, in
    > >
    > > "goodness" =~ /[v]/
    > >
    > > I'd like v to be interepreted not as a literal but as a variable name.
    > >
    > > Thanks
    > > basi
    > >

    >
    > "goodness" =~ /#{v}/
     
    basi, Aug 17, 2005
    #5
    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. Replies:
    17
    Views:
    1,937
    Chris Uppal
    Nov 16, 2005
  2. sunny
    Replies:
    1
    Views:
    483
    Salt_Peter
    Dec 7, 2006
  3. Pallav singh
    Replies:
    0
    Views:
    415
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    435
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    475
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page