From: William James Date: 2008-09-19T04:02:31+09:00 Subject: Re: Suggestion for string parsing On Sep 18, 3:48 am, Me Me wrote: > Hi all, > I would like to know if there's a better way to parse a string and > assing values to variables; > > Ex: > > Client=MPEG-4,390000,700000,24000 > > I can do > > line =~ /(\w*)=([0-9A-Za-z -.:]*),([0-9]*),([0-9]*),([0-9]*)/ > > and > > var1 = $1 > var2 = $2 > var3 = $3 > var4 = $4 > var4 = $5 > > But I'm sure there's a better way, even considering that the number of > parameters can increase and I don't want to write a long regular > expression rule, that is hard to read. s = "Client=MPEG-4,390000,700000,24000" ==>"Client=MPEG-4,390000,700000,24000" if s =~ /^\w+=\S+(,\d+)+$/ vars = s.split( /[=,]/ ) end ==>["Client", "MPEG-4", "390000", "700000", "24000"]