On Tue, Mar 4, 2014 at 1:05 AM, Michele La Monaca <mikele.chic...@lamonaca.net> wrote: >>> (define (my-own-irregex-replace irx s . o) >>> (let ((m (irregex-search irx s))) >>> (and m (string-append >>> (substring s 0 (irregex-match-start-index m 0)) >>> (apply string-append (reverse (irregex-apply-match m o))) >>> (substring s (irregex-match-end-index m 0) (string-length >>> s)))))) > > After some pondering I realized that it would be valuable to provide > this primitive in the library: > > (define (irregex-replace-match m str o) > (string-append > (substring str 0 (irregex-match-start-index m 0)) > (apply string-append (reverse (irregex-apply-match m o))) > (substring str (irregex-match-end-index m 0) (string-length str))))
After some more mulling, I concluded that it would be even more convenient to have a generalised version of irregex-replace-match which also accepts lists of matches: (irregex-replace-match match-or-list-of-matches str o) On top of that, it would be easy to build irregex-replace/all semantics, too: (define (irregex-replace/all irx str . o) (let ((ms (irregex-search/all irx str))) (if (pair? ms) (irregex-replace-match ms str o) str))) (define (my-irregex-replace irx str . o) (let ((ms (irregex-search/all irx str))) (and (pair? ms) (irregex-replace-match ms str o)))) It would also be possible to easily implement less common actions like that: (define (irregex-replace/2nd-and-4th irx str . o) (let ((ms (irregex-search/all irx str 4))) (if (pair? ms) (irregex-replace-match (list (list-ref ms 1) (list-ref ms 3)) str o) str))) Michele _______________________________________________ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users