| Paste number 24849: | Regular expression engine in 14 lines of Python |
| Pasted by: | psykotic |
| When: | 6 years, 8 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+J69 |
| Channel: | None |
| Paste contents: |
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
# c(a|d)+r
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
Annotations for this paste:
| Annotation number 1: | 22222 |
| Pasted by: | 11111 |
| When: | 6 years, 8 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+J69/1 |
| Paste contents: |
33333
| Annotation number 2: | 22222 |
| Pasted by: | 11111 |
| When: | 6 years, 8 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+J69/2 |
| Paste contents: |
33333
| Annotation number 3: | Sir |
| Pasted by: | Martin |
| When: | 6 years, 8 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+J69/3 |
| Paste contents: |
sdkao| Annotation number 4: | multiple |
| Pasted by: | Deepak |
| When: | 6 years, 8 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+J69/4 |
| Paste contents: |
23233| Annotation number 5: | test |
| Pasted by: | jens |
| When: | 6 years, 8 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+J69/5 |
| Paste contents: |
testtext| Annotation number 6: | 24849 |
| Pasted by: | 24849 |
| When: | 6 years, 4 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+J69/6 |
| Paste contents: |
24849| Annotation number 7: | oppo |
| Pasted by: | dyrtrt |
| When: | 6 years, 4 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+J69/7 |
| Paste contents: |
ertret