unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: William James <w_a_x_man@yahoo.com>
To: guile-user@gnu.org
Subject: regex-split for Guile
Date: Mon, 7 Mar 2011 06:57:00 -0800 (PST)	[thread overview]
Message-ID: <135915.35634.qm@web112617.mail.gq1.yahoo.com> (raw)

Tested under Guile 1.8.7.


(define (regex-split regexp str . options)
  (let ((keep #f) (trim #f))
    (if (member 'keep options)
      (begin (set! options (delete 'keep options))
             (set! keep #t)))
    (if (member 'trim options)
      (begin (set! options (delete 'trim options))
             (set! trim #t)))
    (let* ((matches (apply list-matches regexp str options))
           (indices
             (append '(0)
               (fold-right
                 (lambda (m acc) (cons (match:start m)
                                   (cons (match:end m) acc))) '()
                 matches)
               (list (string-length str))))
           (substrings
              (pair-fold-right
                (lambda (lst accum)
                  (if (or (even? (length lst))
                          (and keep (> (length lst) 1)))
                    (cons (apply substring str (take lst 2)) accum)
                    accum))
                '()
                indices)))
      (if trim
        (reverse! (drop-while string-null?
          (reverse! (drop-while string-null? substrings))))
        substrings))))



guile> (regex-split "[-x]+" "foo--x--bar---what--")

("foo" "bar" "what" "")

guile> (regex-split "[-x]+" "foo--x--bar---what--"  'trim)

("foo" "bar" "what")

guile> (regex-split "[-x]+" "foo--x--bar---what"  'keep)

("foo" "--x--" "bar" "---" "what")



      



             reply	other threads:[~2011-03-07 14:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-07 14:57 William James [this message]
2011-03-12  2:08 ` regex-split for Guile Neil Jerram
  -- strict thread matches above, loose matches on Subject: below --
2011-03-14 14:54 William James

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=135915.35634.qm@web112617.mail.gq1.yahoo.com \
    --to=w_a_x_man@yahoo.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).