chicken-users
[Top][All Lists]
Advanced

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

irregex match objects


From: asebian
Subject: irregex match objects
Date: Fri, 4 Mar 2022 00:21:12 +0100

Hi,
I am new to chicken (and scheme in general) and got stuck. (I am not
part of the mailing list yet, so please add my address manually in
case you reply to the list.)

I want to swap two submatches of a regex and came up with the idea of
using named matches to detect what actually was found by analyzing the
match object and reacting accordingly. Unluckily I could not find a
lot of details about match objects in the documentation.

1) I assumed that SRE named match identifiers are strings.
2) I assumed that `irregex-replace/all` will pass a match object for
each individual match to its procedure argument (and that the
procedure will not get called otherwise).
3) I assumed that `irregex-match-names` returns an associative list of
all named matches occuring in the pattern independent of the actual
match.

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?

After further investigations I've got the impression that the match
objects of `irregex-replace/all` are somehow different from the ones
of `irregex-search` for instance.

Is the range of possible (not necessarily valid) index argument for
`irregex-match-substring` less equal than
`irregex-match-num-submatches`? (0: complete match, 1 to num:
submatches)

Is there an easy way to read a SRE from a string or do I have to stick
with PCRE for that?

I hope someone can point me in the right direction.

Cheers!
Asebian



reply via email to

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