Paste number 24849: Regular expression engine in 14 lines of Python Pasted by: psykotic When: 14 years, 1 month ago Share: Tweet this! | http://paste.lisp.org/+J69 Channel: None Paste contents: Raw Source | XML | Display As text/plain image/svg+xml text/html application/xhtml+xml

def iconcat ( xs, ys ) : for x in xs: yield x for y in ys: yield y def nil ( s ) : yield s def seq ( l, r ) : return lambda s: ( sr for sl in l ( s ) for sr in r ( sl ) ) def alt ( l, r ) : return lambda s: iconcat ( l ( s ) , r ( s ) ) def star ( e ) : return lambda s: iconcat ( nil ( s ) , seq ( e, star ( e ) ) ( s ) ) def plus ( e ) : return seq ( e, star ( e ) ) def char ( c ) : def match ( s ) : if s and s [ 0 ] == c: yield s [ 1: ] return match e = seq ( char ( 'c' ) , seq ( plus ( alt ( char ( 'a' ) , char ( 'd' ) ) ) , char ( 'r' ) ) ) import sys s = sys.stdin.readline ( ) [ :-1 ] for r in e ( s ) : print "Match with remainder:" , r