On Sep 18, 2008, at 8:46 AM, Me Me wrote:

>> What is the *actual* problem?  If it is what you said ("I would  
>> like to
>> know if there's a better way to parse a string and assing values to
>> variables;") then RegEx is a fine solution.  If you reject a good
>> solution and seek something else, then it can only be that you are
>> actually seeking a solution to a different problem.  So, what are you
>> *really* looking for?
>
> I'm quite new to Ruby and I can understand that athere are better  
> way to
> do things, what I would like to avoid is to write something like this
> (that works)
>
> line =~ /(\w*)=([0-9A-Za-z -.:]*),([0-9A-Za-z -.:]*),([0-9A-Za-z
> -.:]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*), 
> ([0-9]*),([0-9]*),([0-9]*),([0-9]*)/

I believe there's a bug in your regex.  I assume you don't really mean  
all characters between space and period in the second character class,  
especially since that includes a comman.

> I wanted to this in one line but in a more mainable way, otherwise I
> could always pars the string char by char.

I would probably do it in two steps.  Match the bit before and after  
the equal sign in one, then split() the after bit on commas:

#!/usr/bin/env ruby -wKU

if "Client=MPEG-4,390000,700000,24000" =~ /\A([^=]+)=([^=]+)\z/
   p [$1, *$2.split(",")]
end

__END__

Here's another idea using StringScanner:

#!/usr/bin/env ruby -wKU

require "strscan"

class SimpleParser
   def initialize(data)
     s       = StringScanner.new(data)
     @values = [ ]

     @values << s.matched        if    s.scan(/\w+/)
     @values << s.matched[1..-1] if    s.scan(/=[0-9A-Za-z \-.:]+/)
     @values << s.matched[1..-1] while s.scan(/,[0-9]+/)
   end

   attr_reader :values
end

p SimpleParser.new("Client=MPEG-4,390000,700000,24000").values

__END__

Hope that gives you some fresh ideas.

James Edward Gray II