From: "Jesús Gabriel y Galán" Date: 2009-02-11T19:39:02+09:00 Subject: Re: Method to get string combinations On Wed, Feb 11, 2009 at 11:22 AM, Christoph Blank wrote: > Hi, > > I have a text like this: > > "This {is|was} a {good|nice} day" and want to generate possible > combinations like: > > This is a good day > This was a good day > This is a nice day > This was a nice day > > I tried using zsh functionality to output this, but it doesn't seem that > easy. > Does anyone know an easy way how to do this in ruby? There was a similar question on this list recently, I'll try to find it later for reference. For a ruby quiz (http://rubyquiz.com/quiz143.html) I built a regexp "generator". It adds a method to the Regexp to generate all possible strings that match the regexp. If you use my code: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/274375 Then this works: irb(main):001:0> require 'quiz143' => true irb(main):002:0> a = "This {is|was} a {good|nice} day" => "This {is|was} a {good|nice} day" irb(main):006:0> s = a.gsub(/\{(.*?)\}/, "(\\1)") => "This (is|was) a (good|nice) day" irb(main):008:0> Regexp.new(s).generate => ["This is a good day", "This is a nice day", "This was a good day", "This was a nice day"] I'm changing your string to a valid regexp, changing all occurrences of {} with () (depending on your case you might want to change that). Then I create a regexp with that and call my method. This supports many constructs from regexps, which might be overkill for your problem. For sure there will be easier solutions for your specific case. Anyway, I hope you find it interesting. Jesus.