From: yermej Date: 2008-05-02T01:10:08+09:00 Subject: Re: splitting with a regex & keeping a ref? On May 1, 9:46 am, Kyle Schmitt wrote: > I'm writing some scripts to help handle some ornery samba servers we > have: part of that is unfortunately reading the config scripts that > have built up over the years. > > I was hoping to use the standard string method as a quick & > not-so-dirty way of parsing the files, given that samba uses a very > simple format. > > #the sample_data variable is defined below > irb(main):sample_data.split(/\[[a-z0-9]+\]/i) > => ["", "\ncomment = shared directory for the shop\npath = > /dept/shop\nvalid u ....(truncated) > Gives good results, but omits what's between the brackets. I expected > that part. > > irb(main):sample_data.split(/(\[[a-z0-9]+\])/i) > => ["", "[shop]", "\ncomment = shared directory for the shop\npath = > /dept/sho ....(truncated) > Neat, gives me the data between the brackets in an element before the > data itself. > > I know quite well I can zip through that array again, but I was > wondering, hoping, that there would be a way of accessing that back > reference in a block as part of the split. > > Is there any way to do that that I'm just missing? > > Thanks, > Kyle > > sample_data=%{[shop] > comment = shared directory for the shop > path = /dept/shop > valid users = @shop @admin > public = no > writable = yes > force group = shop > create mask = 0770 > [bob] > comment = User files for bob > path = /users/bob > valid users = bob @admin > public = no > writable = yes > create mask = 0770} I think you might want scan instead of split. sample_data.scan( /(\[[a-z0-9]+\])([^\[]*)/i) do |share, opts| # create your hash or whatever here end