Regex in reverse - decompose regular expressions The Problem

I have a bunch of regular expressions that I need to use in some code, but I'm using a programming language that doesn't support regex! Luckily, I know that the test string will have a maximum length and will be composed of printable ASCII only.

The Challenge

You must input a regex and a number n, and output every string composed of printable ASCII (ASCII codes 32 to 126 inclusive,  to ~, no tabs or newlines) of length less than or equal to n that matches that regex. You may not use built-in regular expressions or regex matching functions in your code at all. Regular expressions will be limited to the following:


Literal characters (and escapes, which force a character to be literal, so \. is a literal ., \n is a literal n (equivalent to just n), and \w is equivalent to w. You do not need to support escape sequences.)
. - wildcard (any character)
Character classes, [abc] means "a or b or c" and [d-f] means anything from d to f (so, d or e or f). The only characters that have special meaning in a character class are [ and ] (which will always be escaped, so don't worry about those), \ (the escape character, of course), ^ at the beginning of the character class (which is a negation), and - (which is a range).
| - the OR operator, alternation. foo|bar means either foo or bar, and (ab|cd)e matches either abe or cde.
* - match the previous token repeated zero or more times, greedy (it tries to repeat as many times as possible)
+ - repeated one or more times, greedy
? - zero or one times
Grouping with parentheses, to group tokens for |, *. +, or ?


The input regex will always be valid (i.e., you do not have to handle input like ?abc or (foo or any invalid input). You may output the strings in any order you would like, but each string must appear only once (don't output any duplicates).

The Test Cases

Input: .*, 1
Output: (empty string), , !, ", ..., }, ~

Input: w\w+, 3
Output: ww, www

Input: [abx-z][^ -}][\\], 3
Output: a~\, b~\, x~\, y~\, z~\

Input: ab*a|c[de]*, 3
Output: c, cd, ce, aa, cde, ced, cdd, cee, aba

Input: (foo)+(bar)?!?, 6
Output: foo, foo!, foofoo, foobar

Input: (a+|b*c)d, 4
Output: ad, cd, aad, bcd, aaad, bbcd

Input: p+cg, 4
Output: pcg, ppcg

Input: a{3}, 4
Output: a{3}

The Winner

This is code-golf, so the shortest code in bytes will win!
Python v2.7 1069 1036 950 925 897 884 871 833 822

This answer seems rather long for a code golf, but there are a lot operators that need to be handled and I know what purpose each byte in this answer does. Since there is no existing answer I submit this as a target for other users to beat. See if you can make a shorter answer :). 

The two main functions are f which parses the regex starting at the ith character, and d which generates the matching strings, using r the sub-regexes we can recursed into, 'a' the array representing the part of the current sub-regex not yet processed, and a string suffix s which represents the part of the string generated so far.

Also check out the sample output and a test harness. 

import sys;V=sys.argv;n=int(V[2]);r=V[1];S=len;R=range;C=R(32,127)
Z=[];z=-1;D='d(r,p,';F='for j in '
def f(i,a):
 if i&gt;=S(r):return a,i
 c=r[i];x=0;I="|)]".find(c)
 if c in"([|":x,i=f(i+1,Z)
 if I+1:return([c,a,x],[a],[c,a])[I],i
 if'\\'==c:i+=1;x=c+r[i]
 return f(i+1,a+[x or c])
def d(r,a,s):
 if S(s)&gt;n:return
 while a==Z:
        if r==Z:print s;return
        a=r[z];r=r[:z]
 e=a[z];p=a[0:z]
 if'|'==a[0]:d(r,a[1],s);d(r,a[2],s)
 elif']'==a[0]:
        g=a[1];N=g[0]=='^';g=(g,g[1:])[N];B=[0]*127;O=[ord(c[z])for c in g]
        for i in R(0,S(g)):
         if'-'==g[i]:exec F+'R(O[i-1],O[i+1]):B[j]=1'
         else:B[O[i]]=1
        for c in C:N^B[c]&lt;1or d(r,Z,chr(c)+s)
 elif' '&gt;e:d(r+[p],e,s)
 else:c=p[:z];exec{'.':F+'C:'+D+'chr(j)+s)','?':D+'s);d(r,p[:z],s)','*':F+'R(0,n+1):d(r,c,s);c+=[p[z]]','+':"d(r,p+['*',p[z]],s)"}.get(e,D+'e[z]+s)')
d(Z,f(0,Z)[0],"")


Note that tabs in the original solution have been expanded. To count the number of characters in the original use unexpand &lt; regex.py | wc.