From: Ezra Zygmuntowicz Date: 2006-06-01T12:47:52+09:00 Subject: Re: Parsing a string using multiple regexs On May 31, 2006, at 4:06 PM, web mail wrote: > > To whomever can help, > > I need to parse a string into parts using multiple regexs, and I'm new > to Ruby, so I have NO idea how to do it. Basically, I'm trying to > parse > tags that users might add on a webpage. > > Example: In my tags input box, user enters the following: > "my first tag", second tag, third_tag fourth_tag > > The order of precedence is quotes, then commas then spaces, so my > results should be a ruby array which contains the values: > > my first tag > second tag > third_tag > fourth_tag > > which means the array will have 4 elements. > > 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