From: Justin Ethier Date: 2007-10-15T00:08:50+09:00 Subject: Re: [QUIZ] Reverse the Polarity (#143) ------=_Part_28009_27215612.1192374530546 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Looks like its been 2 days... Did not have a lot of time this weekend, so my solution just implements operators used by the author: require 'strscan' class Regexp # Create a list of all values matching the regex. # Currently only supports groupings and '|' def generate regex = self.inspect regex = regex.slice(1, regex.size - 2) # Remove leading/trailing '/'' s = StringScanner.new(regex) # Build a list containing each grouping, and blocks in between groups = [] result = '' while (result = s.scan_until(/\(|\)/)) != nil result = result.sub("(", "") # Does not support '\' escape chars if result.size > 0 if result[-1].chr == ")" groups << result.split("|") groups[-1][-1] = groups[-1][-1].sub(")", "") else groups << [result] end end end # Create all combinations of those groups and return them find_list_combinations(groups) end # Return an array of all combinations of values in given list of lists def find_list_combinations(lists) lists.reverse! results = lists.pop list = lists.pop while list != nil new_results = [] for result in results for item in list new_result = Array.new([result]) new_result << item new_results << new_result end end results = new_results list = lists.pop end new_results.map{|list| list.flatten.join } end end Examples of usage: /(lovely|delicious|splendid)(food|snacks|munchies)/.generate /Hello, my name is (Linus|Yukihiro|Dennis) (Torvalds|Matsumoto|Ritchie)/.generate Thanks, Justin On 10/12/07, Ruby Quiz wrote: > > The three rules of Ruby Quiz: > > 1. Please do not post any solutions or spoiler discussion for this quiz > until > 48 hours have passed from the time on this message. > > 2. Support Ruby Quiz by submitting ideas as often as you can: > > http://www.rubyquiz.com/ > > 3. Enjoy! > > Suggestion: A [QUIZ] in the subject of emails about the problem helps > everyone > on Ruby Talk follow the discussion. Please reply to the original quiz > message, > if you can. > > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > by Benjohn Barnes > > Usually, you write a regular expression and then search for it in a text > string. > How about doing the opposite? Given a regular expression, generate all of > the > strings that it will match. > > Rather amazingly, this has independently come up a couple of times in > conversations with friends, so perhaps it's actually useful. I wanted to > use it > to specify possible domain names for a potential new company... > > /(lovely|delicious|splendid)(food|snacks|munchies)/.generate > => [lovelyfood, deliciousfood, splendidfood, > lovelysnacks, delicioussnacks, splendidsnacks, > lovelymunchies, deliciousmunchies, splendidmunchies] > > The full regular expression grammar is pretty complex, so maybe you'll > want to > just go with a small subset. :-) > > ------=_Part_28009_27215612.1192374530546--