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 , so the shortest code in bytes will win!

  • \$\begingroup\$ Are we allowed to support escape sequences? Then this is trivial. \$\endgroup\$ – John Dvorak Apr 5 '14 at 13:39
  • 3
    \$\begingroup\$ Your explanation of | makes very little sense. It doesn't seem to handle nested groups or a|b|c. What's wrong with using the standard explanations in terms of how strongly concatenation and alternation bind? (And you have no excuse for not using the sandbox) \$\endgroup\$ – Peter Taylor Apr 5 '14 at 13:44
  • 1
    \$\begingroup\$ @PeterTaylor Actually, he has an excuse: meta.codegolf.stackexchange.com/q/1305/9498 \$\endgroup\$ – Justin Apr 5 '14 at 15:15
  • 2
    \$\begingroup\$ Judging by your examplesthe pattern has to match the entire string? (As opposed to a substring) \$\endgroup\$ – Martin Ender Apr 5 '14 at 21:01
  • 3
    \$\begingroup\$ @KyleKanos It's a shame real world problems don't make you think you ought to learn regular expressions. :P But they are not as inaccessible as they might seem: regular-expressions.info/tutorial.html \$\endgroup\$ – Martin Ender Apr 7 '14 at 19:34

Haskell 757 705 700 692 679 667

import Data.List
data R=L Char|A R R|T R R|E
h=[' '..'~']
k('\\':c:s)a=k s$c:a
k(c:s)a=k s$c:a
c#E=L c
c#r=A(L c)r
o(a,b)=(foldr(#)E a,b)
t%n=A(t%(n-1))$T t$t%(n-1)
d s n=m(fst$r s)[[]] where{m E a=a;m(L c)a=[b++[c]|b<-a,length b<n];m(A r s)x=nub$(m r x)++m s x;m(T r s)a=m s$m r a;r s=w$e s E;w(u,'|':v)=(\(a,b)->(A u a,b))$r v;w x=x;e(')':xs)t=(t,xs);e s@('|':_)t=(t,s);e s@(c:_)t=g t$f$b s;e[]t=(t,[]);g t(u,v)=e v$T t u;f(t,'*':s)=(t%n,s);f(t,'+':s)=(T t$t%n,s);f(t,'?':s)=(A t E,s);f(t,s)=(t,s);b('(':s)=r s;b('\\':s:t)=(L s,t);b('.':s)=o(h,s);b('[':s)=o$k s[];b(s:t)=(L s,t)}


ghci> d ".*" 1
[""," ","!","\"","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~"]
ghci> d "w\\w+" 3
ghci> d "[abx-z][^ -}][\\\\]" 3
ghci> d "ab*a|c[de]*" 3
ghci> d "(foo)+(bar)?!?" 6
ghci> d "(a+|b*c)d" 4
ghci> d "p+cg" 4
ghci> d "a{3}" 4

Explanation: this one's a textbook regex implementation. R is the regex type, with constructors A (alternate), L (literal), T (then) and E (empty/epsilon). The usual 'Star' doesn't appear because I inline it as alternates during the parse (see '%'). 'm' runs the simulation. The parser (start at 'r s=....') is just recursive descent; 'k' parses ranges. The function '#' expands ranges into alternations.


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>=S(r):return a,i
 if c in"([|":x,i=f(i+1,Z)
 if I+1:return([c,a,x],[a],[c,a])[I],i
 return f(i+1,a+[x or c])
def d(r,a,s):
 if S(s)>n:return
 while a==Z:
        if r==Z:print s;return
        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'
        for c in C:N^B[c]<1or d(r,Z,chr(c)+s)
 elif' '>e:d(r+[p],e,s)

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

  • 9
    \$\begingroup\$ I've never seen python look that horrible. \$\endgroup\$ – user80551 Apr 12 '14 at 18:17
  • \$\begingroup\$ Can't you change def E(a,b):c=a[:];c.extend(b);return c to E=lambda a,b:a[:].extend(b) , ditto for A \$\endgroup\$ – user80551 Apr 12 '14 at 18:21
  • \$\begingroup\$ Apparently not, as .extend(b) does not return anything. \$\endgroup\$ – gmatht Apr 12 '14 at 18:51
  • 1
    \$\begingroup\$ For the elif isinstance(e,str):, I believe you could change the code inside to: exec{'.':'for c in C:d(r,p,s+chr(c))','?':'d(r,p,s);d(r,p[:z],s)','*':'''c=p[:z]#newline for i in R(0,n+1):d(r,c,s);c+=[p[z]]''','+':"d(r,p+['*',p[z]],s)",'\\':'d(r,p,e[1]+s)'}.get(e,'d(r,p,e+s)')(note that the #newline is a newline) (source: stackoverflow.com/a/103081/1896169) \$\endgroup\$ – Justin Apr 12 '14 at 19:30
  • 1
    \$\begingroup\$ If you could find more places to use the exec trick, we could easily change your code into unreadable code :-) \$\endgroup\$ – Justin Apr 12 '14 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.