From: RichardOnRails Date: 2007-11-25T06:30:02+09:00 Subject: Re: New at regexp and Ruby need help on parsing a string. On Nov 23, 10:29 am, Peter Vanderhaden wrote: > Raul, > Interesting solution. One question, how did you print the output? I'm > a newbie and the output I got when I tried your solution came out like: > > andstuffnice thingsgirlsboysnotbad girlsgreasy boys > > I used puts smoking_table. I'm assuming that's not the correct way to > do it. > Thanks, > PV > > > > > > Raul Parolari wrote: > > Gabra Kadabra wrote: > >> I'm building a little test console for a ruby project. When using a > >> function I might get something like this: > > >> input_string ="and stuff and nice things not bad girls not greasy boys > >> and girlsandboys" > > >> As you already have guessed, I want the following in some kind of > >> format: > > >> smoking_table = {"and" => ["stuff", "nice things", "girlsandboys"], > >> "not" => ["bad girls","greasy boys"]} > > >> Thus, a regexp that splits a string on code words like "and" and "not" > >> is what I need. > >> Please help me > > > # One possible implementation is: > > > smoking_table = { :and => [], :not => [] } > > > str.scan(/ (and|not) (.*?) (?= \band|\bnot|$) /x) do |k, v| > > smoking_table[k.to_sym].push(v.strip) > > end > > > => {:and => ["stuff", "nice things", "girlsandboys"], > > :not => ["bad girls", "greasy boys"]} > > > I hope that this works for you, > > > Raul > > -- > Posted viahttp://www.ruby-forum.com/.- Hide quoted text - > > - Show quoted text - p smoking_table (Same as stud's example). HTH, Richard