unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Neil Jerram <neil@ossau.uklinux.net>
To: William James <w_a_x_man@yahoo.com>
Cc: guile-user@gnu.org
Subject: Re: regex-split for Guile
Date: Sat, 12 Mar 2011 02:08:12 +0000	[thread overview]
Message-ID: <87ei6dt7jn.fsf@ossau.uklinux.net> (raw)
In-Reply-To: <135915.35634.qm@web112617.mail.gq1.yahoo.com> (William James's message of "Mon, 7 Mar 2011 06:57:00 -0800 (PST)")

William James <w_a_x_man@yahoo.com> 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	other threads:[~2011-03-12  2:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-07 14:57 regex-split for Guile William James
2011-03-12  2:08 ` Neil Jerram [this message]
  -- 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=87ei6dt7jn.fsf@ossau.uklinux.net \
    --to=neil@ossau.uklinux.net \
    --cc=guile-user@gnu.org \
    --cc=w_a_x_man@yahoo.com \
    /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).