Skip to content

Instantly share code, notes, and snippets.

@drewc
Created August 22, 2019 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewc/ea277d14144ad0691d10d5d7f18a1f92 to your computer and use it in GitHub Desktop.
Save drewc/ea277d14144ad0691d10d5d7f18a1f92 to your computer and use it in GitHub Desktop.
(user)> (def (pregexp-match* re str (start 0) (end (string-length str)))
(def (%m start end)
(if (= start end)
'() (let ((first (pregexp-match-positions re str start end)))
(if (not first) '()
(cons (cadr first) (%m (+ 1 start) end))))))
(let ((idxs (delete-duplicates (%m start end) equal?)))
(map (lambda (idx) (substring str (car idx) (cdr idx))) idxs)))
#!void
(user)> (pregexp-match* "(a)" "aa ab ac")
("a" "a" "a" "a")
@drewc
Copy link
Author

drewc commented Aug 22, 2019

(user)> (def (pregexp-match* re str (start 0) (end (string-length str)))
            (def (%m start end)
                (if (= start end)
                    '() (let ((first (pregexp-match-positions re str start end)))
                          (if (not first) '() 
                              (cons (if (null? (cddr first)) 
                                        (list (cadr first)) (cdr first)) (%m (+ 1 start) end))))))
          (let ((idxs (delete-duplicates (apply append (%m start end)) equal?)))
           (map (lambda (idx) (substring str (car idx) (cdr idx))) idxs) ))
#!void
(user)> (pregexp-match* "(a)(a)" "aa aa aa")
("a" "a" "a" "a" "a" "a")
(user)> `

@drewc
Copy link
Author

drewc commented Aug 22, 2019

(user)> 
      (def (pregexp-match* re str (start 0) (end (string-length str)))
            (def (%m start end)
                (if (= start end)
                    '() 
                    (let ((first (pregexp-match-positions re str start end)))
                      (if (not first) '() 
                          (cons (if (null? (cddr first)) 
                                    (cadr first) (cdr first)) (%m (+ 1 start) end))))))
          (let ((idxs (delete-duplicates (flatten1 (%m start end)) equal?)))
            (map (lambda (idx) (substring str (car idx) (cdr idx))) idxs) ))
#!void
(user)> (pregexp-match* "(a)(a)" "aa aa aa")
("a" "a" "a" "a" "a" "a")
(user)> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment