chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: irregex match objects


From: felix . winkelmann
Subject: Re: irregex match objects
Date: Fri, 04 Mar 2022 14:57:13 +0100

> Under these assumptions I concluded that it should be possible to
> detect what has been matched by checking whether the indices of the
> assoc. list are valid in the current match object, stopping after the
> first successful test.
>
> (import  (chicken irregex))
> ;;; swapping fred and wilma
> (irregex-replace/all '(: bow
>                          (or (=> "f" (w/nocase "fred"))
>                              (=> "w" (w/nocase "wilma")))
>                          eow)
>                      "fred dino wilma barney"
>                      (lambda (match-obj)
>                        (assert match-obj)
>                        (letrec ((iter (lambda (alist)
>                                         (unless (null? alist)
>                                           (let ((pair (car alist)))
>                                             (assert (pair? pair))
>                                             (if
> (irregex-match-valid-index? 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)))))
>
> To my suprise the result is "Fred dino Fred barney" instead of "Wilma
> dino Fred barney". What did I miss?

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.


felix




reply via email to

[Prev in Thread] Current Thread [Next in Thread]