[plt-scheme] pregexp-replace-all ?

From: Dustin Mulcahey (dem5302 at cs.rit.edu)
Date: Fri Aug 20 13:24:00 EDT 2004

I'm still fairly new to Scheme, and I have been using plt scheme at 
work lately to do some analysis on our code base.

While regexp-replace* returns a list of all matches in a particular 
string, it does not give subpattern matches.  Also, there is no 
pregexp-replace* function in pregexp.ss.

I propose pregexp-replace-all to become a part of the pregexp.ss 
library.  It simply gives the results of pregexp-match on all matching 
portions of a particular string or input port in list form.  Its simple 
enough for anyone to implement, but I believe it to be handy enough to 
be included.

Implementation details:

(define pregexp-match-all
   (lambda (regex string)
     (letrec
         ((pregexp-match-all
           (lambda (regex string pos)
             (let ((endpos (pregexp-match-positions regex string pos)))
               (if (eq? endpos #f) '()
                   (cons (pregexp-match regex string pos)
                         (pregexp-match-all regex string (cdar 
endpos))))))))
       (pregexp-match-all regex string 0))))

Dustin Mulcahey



Posted on the users mailing list.