------art_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  elf.inspect
    regex  egex.slice(1, regex.size - 2) # Remove leading/trailing '/''
    s  tringScanner.new(regex)

    # Build a list containing each grouping, and blocks in between
    groups  ]
    result  '
    while (result  .scan_until(/\(|\)/)) ! il
      result  esult.sub("(", "") # Does not support '\' escape chars

      if result.size > 0
        if result[-1].chr ")"
          groups << result.split("|")
          groups[-1][-1]  roups[-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  ists.pop
    list  ists.pop

    while list ! il
      new_results  ]
      for result in results
        for item in list
          new_result  rray.new([result])
          new_result << item
          new_results << new_result
        end
      end

      results  ew_results
      list  ists.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 <james / grayproductions.net> 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. :-)
>
>

------art_28009_27215612.1192374530546--