matching regular expression

J

Joe Van Dyk

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
 
M

Malte Milatz

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
 
M

mark sparshatt

Joe said:
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
 
S

Simon Strandgaard

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$
 
J

Joe Van Dyk

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

Robert Klemme

Simon Strandgaard said:
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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top