Reverse Regular expressions The idea here is to write a little segment that will spit out regular expression patterns when given a string that matches them.

As you might have guessed, this will be a popularity contest, and I'll accept whichever answer has the most upvotes by March 7th

pseudocode Example:

print(reverseRegex('dog')); 
//prints /[a-z]{3}/, /dog/, and a bunch other stuff


EDIT: The regular expressions spat out should not exceed 3 times the input string's length
Ruby

def esc chr
  '\\[]'.index(chr) ? "\\#{chr}" : chr
end

def reverse_regex str
  ords = str.chars.to_a.uniq.map(&amp;:ord).sort
  prev = ords.first
  ranges = ords.slice_before{|x|
    prev, prev2 = x, prev
    prev2 + 1 != x
  }.map{|x| x[0]..x[-1]}

  reversed = "[#{ranges.map{|c|esc(c.min.chr) + (c.min == c.max ? '' : (c.min + 1 == c.max ? '' : '-') + esc(c.max.chr))}*''}]{#{str.size}}"
  reversed.size &gt; str.size * 3 ? /#{Regexp.escape str}/ : /#{reversed}/
end


Sample outputs:

reverse_regex 'dogs are delicious'
  =&gt; /[ ac-egilorsu]{18}/
reverse_regex 'a'
  =&gt; /a/
reverse_regex ''
  =&gt; //
reverse_regex 'This is a test: []{}.*potato'
  =&gt; /[ *.:T\[\]aehiopst{}]{28}/


Now with ranges!

These regexen match the entire string (if you added ^...$ it would still match, but I'm too lazy to do that and add the required length check). Also, I'm assuming the /s don't count for length. (If they did, it would be impossible for input of empty string.)