> I haven't looked very deep into this, but it seems the replacement
> lambda is called with the same match object for every substitution.
> You don't seem to distinguish between the different matches in any
> way - the result of the lambda will always be the same.
Thanks, felix for having a glimpse. And an even bigger 'thank you' for
initializing and sharing chicken!
Problem solved!
`irregex-match-valid-index?` does not tell us whether the match
happened at a certain index, and the documentation even says so:
"Returns #t iff [sic] the index-or-name named submatch or index is
defined in the match object."
Valid means defined here - not matched.
I have written a function that dumps the content of the match object.
Of course all the indices appearing in the dump are defined as
indicated by *. Actually I am not quite sure what the purpose of
`irregex-match-valid-index?` is.
Cheers!
#!/usr/bin/env -S csi -s
(import (chicken format)
(chicken irregex)
(chicken string)
srfi-1)
(define (dump match-obj port)
(let ((match? (irregex-match-data? match-obj)))
(when match?
(fprintf port
"match-data? ~A~N"
match?)
(letrec ((num (irregex-match-num-submatches match-obj))
(names (irregex-match-names match-obj))
(iter (lambda (index)
(when (<= index num)
(fprintf port
"~A ~A, ~A: '~A'~N"
(if (irregex-match-valid-index?
match-obj index)
#\*
#\space)
index
(string-intersperse
(map car
(filter (lambda (pair)
(= index (cdr pair)))
names))
", ")
(irregex-match-substring match-obj
index))
(iter (add1 index))
(fprintf port
"num-submatches: ~A~N"
num)
(iter 0)))
(display (make-string 32 #\-) port)
(newline port))
(void))
(display
(irregex-replace/all
'(: bow
(or (=> "f" (w/nocase "fred"))
(=> "w" (w/nocase "wilma")))
eow)
"fred dino wilma barney"
(lambda (match-obj)
(dump match-obj (current-output-port))
(letrec ((iter (lambda (alist)
(unless (null? alist)
(let ((pair (car alist)))
(assert (pair? pair))
(if (irregex-match-substring match-obj
(cdr pair))
(let ((name (car pair)))
(assert (string? name))
(cond
((string=? name "f") "Wilma")
((string=? name "w") "Fred")
(else (error "unknown named match"
name
(iter (cdr alist
(iter (irregex-match-names match-obj))
(newline)