matching regular expression

Discussion in 'Ruby' started by Joe Van Dyk, Mar 7, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    I have a string that contains a list of keys and values.

    "key1: a_value, key2: another_value, key3: yet_another_value"

    I have an array of ["key1", "key2", "key3"].

    I'd like to loop through and find all the values in the string that
    have a key in the key array.

    My first attempt was:

    keys = ["key1", "key2", "key3"]
    my_str = "key1: a_value, key2: another_value, key3: yet_another_value"
    keys.each do |key|
    my_str =~ Regexp.new("#{key}: \\w+")
    puts "the value for #{key} was #{$1}"
    end

    But that's not working properly.

    Any ideas?

    Thanks,
    Joe
    Joe Van Dyk, Mar 7, 2005
    #1
    1. Advertising

  2. Joe Van Dyk

    Malte Milatz Guest

    Joe Van Dyk:
    > keys = ["key1", "key2", "key3"]
    > my_str = "key1: a_value, key2: another_value, key3: yet_another_value"
    > keys.each do |key|
    > my_str =~ Regexp.new("#{key}: \\w+")
    > puts "the value for #{key} was #{$1}"
    > end


    You forgot the parantheses in the regular expression:

    my_str =~ /#{key}: (\w+)/

    Malte
    Malte Milatz, Mar 7, 2005
    #2
    1. Advertising

  3. Joe Van Dyk wrote:
    > I have a string that contains a list of keys and values.
    >
    > "key1: a_value, key2: another_value, key3: yet_another_value"
    >
    > I have an array of ["key1", "key2", "key3"].
    >
    > I'd like to loop through and find all the values in the string that
    > have a key in the key array.
    >
    > My first attempt was:
    >
    > keys = ["key1", "key2", "key3"]
    > my_str = "key1: a_value, key2: another_value, key3: yet_another_value"
    > keys.each do |key|
    > my_str =~ Regexp.new("#{key}: \\w+")


    use this instead
    my_str =~ Regexp.new("#{key}: (\\w+)")
    you need the brackets in order to capture the substring

    > puts "the value for #{key} was #{$1}"
    > end
    >
    > But that's not working properly.
    >
    > Any ideas?


    HTH

    --
    Mark Sparshatt
    mark sparshatt, Mar 7, 2005
    #3
  4. On Tue, 8 Mar 2005 06:04:48 +0900, Joe Van Dyk <> wrote:
    > My first attempt was:

    [snip]
    > But that's not working properly.
    >
    > Any ideas?



    try this

    Simon-Strandgaards-computer:~/code/experiments/ruby simonstrandgaard$
    ruby re1.rb
    the value for key1 was a_value
    the value for key2 was another_value
    the value for key3 was yet_another_value
    Simon-Strandgaards-computer:~/code/experiments/ruby simonstrandgaard$
    expand -t2 re1.rb
    keys = ["key1", "key2", "key3"]
    my_str = "key1: a_value, key2: another_value, key3: yet_another_value"
    res = my_str.scan(/(\w+):\s(\w+)/)
    res.each do |key, val|
    puts "the value for #{key} was #{val}"
    end
    Simon-Strandgaards-computer:~/code/experiments/ruby simonstrandgaard$


    --
    Simon Strandgaard
    Simon Strandgaard, Mar 7, 2005
    #4
  5. Joe Van Dyk

    Joe Van Dyk Guest

    On Tue, 8 Mar 2005 06:13:24 +0900, mark sparshatt
    <> wrote:
    > Joe Van Dyk wrote:
    > > I have a string that contains a list of keys and values.
    > >
    > > "key1: a_value, key2: another_value, key3: yet_another_value"
    > >
    > > I have an array of ["key1", "key2", "key3"].
    > >
    > > I'd like to loop through and find all the values in the string that
    > > have a key in the key array.
    > >
    > > My first attempt was:
    > >
    > > keys = ["key1", "key2", "key3"]
    > > my_str = "key1: a_value, key2: another_value, key3: yet_another_value"
    > > keys.each do |key|
    > > my_str =~ Regexp.new("#{key}: \\w+")

    >
    > use this instead
    > my_str =~ Regexp.new("#{key}: (\\w+)")
    > you need the brackets in order to capture the substring
    >
    > > puts "the value for #{key} was #{$1}"
    > > end
    > >
    > > But that's not working properly.
    > >
    > > Any ideas?

    >
    > HTH
    >


    Argh, you're right. Thanks.
    Joe Van Dyk, Mar 7, 2005
    #5
  6. "Simon Strandgaard" <> schrieb im Newsbeitrag
    news:...
    > On Tue, 8 Mar 2005 06:04:48 +0900, Joe Van Dyk <>

    wrote:
    > > My first attempt was:

    > [snip]
    > > But that's not working properly.
    > >
    > > Any ideas?

    >
    >
    > try this
    >
    > Simon-Strandgaards-computer:~/code/experiments/ruby simonstrandgaard$
    > ruby re1.rb
    > the value for key1 was a_value
    > the value for key2 was another_value
    > the value for key3 was yet_another_value
    > Simon-Strandgaards-computer:~/code/experiments/ruby simonstrandgaard$
    > expand -t2 re1.rb
    > keys = ["key1", "key2", "key3"]
    > my_str = "key1: a_value, key2: another_value, key3: yet_another_value"
    > res = my_str.scan(/(\w+):\s(\w+)/)
    > res.each do |key, val|
    > puts "the value for #{key} was #{val}"
    > end
    > Simon-Strandgaards-computer:~/code/experiments/ruby simonstrandgaard$


    .... and if there are multiple lookups on the same string then using the
    code above to create a hash from the string might be a good idea. You can
    then do something like:

    tmp_hash = ...
    result_hash = keys.inject({}){|h,k| h[k]=tmp_hash[k]; h}

    Kind regards

    robert
    Robert Klemme, Mar 8, 2005
    #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. VSK
    Replies:
    2
    Views:
    2,289
  2. Codex Twin
    Replies:
    1
    Views:
    686
    Wessel Troost
    Apr 18, 2005
  3. Replies:
    6
    Views:
    851
    John C. Bollinger
    Oct 7, 2005
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    846
    Alan Moore
    Dec 2, 2005
  5. Edwin G. Castro
    Replies:
    3
    Views:
    3,673
    Edwin G. Castro
    Sep 17, 2004
Loading...

Share This Page