guile-user
[Top][All Lists]
Advanced

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

Re: regex-split for Guile


From: Neil Jerram
Subject: Re: regex-split for Guile
Date: Sat, 12 Mar 2011 02:08:12 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

William James <address@hidden> writes:

> (define (regex-split regexp str . options)

Thanks for posting that!  For fun/interest, here's an alternative
implementation that occurred to me.

       Neil


(use-modules (ice-9 regex)
             (ice-9 string-fun))

(define (regex-split regex str . opts)
  (let* ((unique-char #\@)
         (unique-char-string (string unique-char)))
    (let ((splits (separate-fields-discarding-char
                   unique-char
                   (regexp-substitute/global #f
                                             regex
                                             str
                                             'pre
                                             unique-char-string
                                             0
                                             unique-char-string
                                             'post)
                   list)))
      (cond ((memq 'keep opts)
             splits)
            (else
             (let ((non-matches (map (lambda (i)
                                       (list-ref splits (* i 2)))
                                     (iota (floor (/ (1+ (length splits)) 
2))))))
               (if (memq 'trim opts)
                   (filter (lambda (s)
                             (not (zero? (string-length s))))
                           non-matches)
                   non-matches)))))))



reply via email to

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