Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > matching regular expression

Reply
Thread Tools

matching regular expression

 
 
Joe Van Dyk
Guest
Posts: n/a
 
      03-07-2005
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


 
Reply With Quote
 
 
 
 
Malte Milatz
Guest
Posts: n/a
 
      03-07-2005
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

 
Reply With Quote
 
 
 
 
mark sparshatt
Guest
Posts: n/a
 
      03-07-2005
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



 
Reply With Quote
 
Simon Strandgaard
Guest
Posts: n/a
 
      03-07-2005
On Tue, 8 Mar 2005 06:04:48 +0900, Joe Van Dyk <(E-Mail Removed)> 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


 
Reply With Quote
 
Joe Van Dyk
Guest
Posts: n/a
 
      03-07-2005
On Tue, 8 Mar 2005 06:13:24 +0900, mark sparshatt
<(E-Mail Removed)> 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.


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-08-2005

"Simon Strandgaard" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Tue, 8 Mar 2005 06:04:48 +0900, Joe Van Dyk <(E-Mail Removed)>

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Regular Expression and string Matching/Replace sanjay010@yahoo.com Java 6 10-07-2005 03:36 AM
Regular Expression matching double quotes Codex Twin ASP .Net 1 04-18-2005 07:22 PM
Using XSLT to Filter out elements with CDATA matching a regular expression Edwin G. Castro XML 3 09-17-2004 05:12 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments