This Web
Other Webs
Schematics
Scheme Links
regexp-split
pregexp-split
(define (regexp-split-inclusive re str) (let ((unmatched-part (lambda (str next-match index) (if (not next-match) (if (< index (string-length str)) (list (substring str index)) '()) (if (not (eq? index (caar next-match))) (list (substring str index (caar next-match))) '()))))) (let loop ((parts '()) (index 0) (next-match (regexp-match-positions re str))) (if (not next-match) (reverse (append (unmatched-part str next-match index) parts)) (loop (cons (substring str (caar next-match) (cdar next-match)) (append (unmatched-part str next-match index) parts)) (cdar next-match) (regexp-match-positions re str (cdar next-match)))))))
> (regexp-split-inclusive " +" "This is a test") ("This" " " "is" " " "a" " " "test")