From: Logan Capaldo Date: 2006-06-02T06:11:10+09:00 Subject: Re: Parsing a string using multiple regexs On Jun 1, 2006, at 12:13 AM, Jeff Pritchard wrote: > Thanks Ezra, > I was sure I would learn something by attempting an answer. I learned > several somethings: > > tags << next answer > (cleaner than using subscripts) > > input.gsub!(/\"(.*?)\"\s*/ ) { tags << $1; "" } > (remove the stuff you find as you find it - brilliant!) > > /\"(.*?)\"\s*/ > (don't forget to escape the quotes and use ? to make .* less greedy) > > # replace all commas with a space > (if you can't get there from here, go someplace else first!) > > tags.concat input.split(/\s/) > (another cool way to shove some more answers into the answer array) > > # strip whitespace from the names >> tags.map! { |t| t.strip } > (instead of making the regexp more complicated, do it simply and then > clean up the results) > > # delete any blank tag names >> tags = tags.delete_if { |t| t.empty? } > (yet another iterator I never heard of before) > > > def parse_tags(input) > ... > end > (modularize everything as you go!) > > > P.S. Ezra, thanks for recommending RimuHosting, they've been great! > > best, > jp > > I must say this has to be the best breakdown of someone else's ruby code I've ever seen. Maybe you should do a ruby-quiz summary sometime. > Ezra Zygmuntowicz wrote: >> On May 31, 2006, at 4:06 PM, web mail wrote: >> >>> >>> I desperately need help. Anyone ? >>> >>> Thanks, >>> >>> Jason >>> >>> -- >>> Posted via http://www.ruby-forum.com/. >>> >> >> >> def parse_tags(input) >> tags = [] >> # pull out the quoted tags >> input.gsub!(/\"(.*?)\"\s*/ ) { tags << $1; "" } >> # replace all commas with a space >> input.gsub!(/,/, " ") >> # get whatever's left >> tags.concat input.split(/\s/) >> # strip whitespace from the names >> tags.map! { |t| t.strip } >> # delete any blank tag names >> tags = tags.delete_if { |t| t.empty? } >> return tags >> end >> >> >> -Ezra > > > -- > Posted via http://www.ruby-forum.com/. >