# Matching regular expression

**URL:** <https://rubytalk.org/t/matching-regular-expression/16837>\
**Category:** ruby-talk\
**Created:** [7 March 2005 21:04 UTC](https://rubytalk.org/t/matching-regular-expression/16837 "2005-03-07T21:04:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [7 March 2005 21:04 UTC](https://rubytalk.org/t/matching-regular-expression/16837/1 "2005-03-07T21:04:48Z")

</div>

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|  
&nbsp;&nbsp;&nbsp;&nbsp;my\_str =~ Regexp.new("#{key}: \\w+")  
&nbsp;&nbsp;&nbsp;&nbsp;puts "the value for #{key} was #{$1}"  
end

But that's not working properly.

Any ideas?

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![Mark10](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@Mark10](https://rubytalk.org/u/Mark10)\
**Post date:** [7 March 2005 21:13 UTC](https://rubytalk.org/t/matching-regular-expression/16837/2 "2005-03-07T21:13:24Z")

</div>

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|  
> &nbsp;&nbsp;&nbsp;&nbsp;my\_str =~ Regexp.new("#{key}: \\w+")

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

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

HTH

> **···**
>
> --  
> Mark Sparshatt

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard2](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Simon\_Strandgaard2](https://rubytalk.org/u/Simon_Strandgaard2)\
**Post date:** [7 March 2005 21:16 UTC](https://rubytalk.org/t/matching-regular-expression/16837/3 "2005-03-07T21:16:54Z")

</div>

> 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|  
&nbsp;&nbsp;puts "the value for #{key} was #{val}"  
end  
Simon-Strandgaards-computer:~/code/experiments/ruby simonstrandgaard$

> **···**
>
> On Tue, 8 Mar 2005 06:04:48 +0900, Joe Van Dyk \<joevandyk@gmail.com\> wrote:
> 
> --  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [7 March 2005 21:17 UTC](https://rubytalk.org/t/matching-regular-expression/16837/4 "2005-03-07T21:17:10Z")

</div>

Argh, you're right. Thanks.

> **···**
>
> On Tue, 8 Mar 2005 06:13:24 +0900, mark sparshatt \<msparshatt@yahoo.co.uk\> 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  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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
