[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] irregex-replace return value
From: |
Michele La Monaca |
Subject: |
Re: [Chicken-users] irregex-replace return value |
Date: |
Tue, 4 Mar 2014 01:05:22 +0100 |
>> (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))))
On top of which is easier to build irregex-replace semantics:
(define (irregex-replace irx str . o)
(let ((m (irregex-search irx str)))
(if m (irregex-replace-match m str o) str)))
(define (my-irregex-replace irx str . o)
(let ((m (irregex-search irx str)))
(and m (irregex-replace-match m str o))))
I would also add this procedure:
(irregex-search/all <irx> <str> [max])
to provide a list of all (or max) matches.
By combining irregex-search/all and irregex-replace-match it's a
breeze to build an "irregex-replace/nth" semantic:
(define (irregex-replace/nth n irx str . o)
(irregex-replace-match (car (reverse (irregex-search/all irx str n))) str o))
(irregex-replace/nth 4 'digit "1-2-3-4-5" "notprime")
=> 1-2-3-notprime-5
Michele