From: "Choi, Junegunn" Date: 2009-02-11T20:53:17+09:00 Subject: Re: Method to get string combinations Hi, this is my code just for unfolding braces in a string. It also tries to handle nested braces like Dir.glob. Seems working fine. def unfold(string) def recursion(input, list) num_groups = 0 input.scan(/\{([^{}]*?)\}/) do | group | prefix, suffix = $`, $' num_groups += 1 tokens = group[0].split(/[,|]/) tokens = [''] if tokens.empty? tokens.each do | token | recursion(prefix + token + suffix, list) end break end list << input if num_groups == 0 end list = [] recursion(string, list) list.uniq end puts unfold("This {is|was} a {good|nice} day") # A more complex one puts unfold('Who is {th{is,at},she,he,it} {?,!}{}')