From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: William James Newsgroups: gmane.lisp.guile.user Subject: regex-split for Guile Date: Mon, 7 Mar 2011 06:57:00 -0800 (PST) Message-ID: <135915.35634.qm@web112617.mail.gq1.yahoo.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1299544160 6058 80.91.229.12 (8 Mar 2011 00:29:20 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 8 Mar 2011 00:29:20 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Mar 08 01:29:13 2011 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PwknX-0006Dq-8h for guile-user@m.gmane.org; Tue, 08 Mar 2011 01:29:12 +0100 Original-Received: from localhost ([127.0.0.1]:34080 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PwknM-0004M9-Co for guile-user@m.gmane.org; Mon, 07 Mar 2011 19:29:00 -0500 Original-Received: from [140.186.70.92] (port=35487 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pwbrs-0008Tf-0c for guile-user@gnu.org; Mon, 07 Mar 2011 09:57:04 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Pwbrq-0003Pc-Me for guile-user@gnu.org; Mon, 07 Mar 2011 09:57:03 -0500 Original-Received: from nm6-vm0.bullet.mail.sp2.yahoo.com ([98.139.91.206]:30059) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1Pwbrq-0003PI-BF for guile-user@gnu.org; Mon, 07 Mar 2011 09:57:02 -0500 Original-Received: from [98.139.91.66] by nm6.bullet.mail.sp2.yahoo.com with NNFMP; 07 Mar 2011 14:57:00 -0000 Original-Received: from [98.139.91.8] by tm6.bullet.mail.sp2.yahoo.com with NNFMP; 07 Mar 2011 14:57:00 -0000 Original-Received: from [127.0.0.1] by omp1008.mail.sp2.yahoo.com with NNFMP; 07 Mar 2011 14:57:00 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 532408.13832.bm@omp1008.mail.sp2.yahoo.com Original-Received: (qmail 36276 invoked by uid 60001); 7 Mar 2011 14:57:00 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1299509820; bh=wuAOFpI5qx7/aZKoB7GMU+0Gep/hbOm86drSjvjjFEk=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type; b=KuuLWj0T/M56Y9zb7qQotyhiTBttoEypX7YyYiSIyLXJEtxBJZtQKT6djIkhMoTjADkTCnfC6tnfzavSIT5E/Jujyj6mZsrkGA5txrfhFcCrX8F9E+YYm6rFwhNIj5ynv/ToUbQKGYKbANxcKOWdMqTRfRAN5YDdVdm+J61QmNc= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type; b=YMvhB74GfG0e3Nvn8eatLTkQqrn28AQ8qgB1tG+nitd1MUuQqRWq+7rq9pww4fLnHDpM+UiYRzrPFuEWpLaT9DQNXzC+2jFjVuRazkOepOAW/UVmVDZ9i1dB3IXPxWsaHSFLKLTJHH1dbNbDjMYNHKwqgoi6HqgACsaKqMfod7U=; X-YMail-OSG: FEsxYiIVM1k5N9fR0t0lxkKbrVdZzi1IdbNa6ZiaEeeJLMl XsQZyZKiC.MbMwpuqprPs9y6g9i1fkSqbZ4EVshmeMmi4wJojJdZxxR9AaY5 PiprTPGXTHnkXawtNQ3qZwus6uVtusivcwZ8iGKDTkImR80NnXflEMPpgoda 0cRpSAdJmqkEGxp2RUUMF1vm3H7v1IGX6GDs20SynNsnfyCsuzwQi_o0Lkdk cagHScreqXULLWTeS20mCgzBA0FmWUb0Mv24a3tWKnA85tzbVRWYsn7Y.6rw BWU_8RmRSnRqDhIedZyGmTypoxJFeeOZoXs6KfRZAj3nN.wE9cnl1mEYZ3WD MJXjg6xdx7fy2DIkDwDRlgKWEZ7DrRPHhrZ2ZSfRkKuK46MEoqAAR1Q-- Original-Received: from [208.100.225.33] by web112617.mail.gq1.yahoo.com via HTTP; Mon, 07 Mar 2011 06:57:00 PST X-Mailer: YahooMailClassic/11.4.20 YahooMailWebService/0.8.109.292656 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 98.139.91.206 X-Mailman-Approved-At: Mon, 07 Mar 2011 19:26:35 -0500 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:8487 Archived-At: 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")