Sorry, there's a typo.
Here it is.

On Thu, Dec 29, 2011 at 5:32 PM, Nala Ginrut <nalagin...@gmail.com> wrote:

> hi guilers!
> It seems like there's no "regexp-split" procedure in Guile.
> What we have is "string-split" which accepted Char only.
> So I wrote one for myself.
>
> ------python code-----
> >>> import re
> >>> re.split("([^0-9])", "123+456*/")
> [’123’, ’+’, ’456’, ’*’, ’’, ’/’, ’’]
> --------code end-------
>
> The Guile version:
>
> ----------guile code-------
> (regexp-split "([^0-9])"  "123+456*/")
> ==>("123" "+" "456" "*" "" "/" "")
> ----------code end--------
>
> Anyone interested in it?
>
>
From 7ecd9cfbb97b436ed9417f4962bc04264bcdb5e4 Mon Sep 17 00:00:00 2001
From: NalaGinrut <nalagin...@gmail.com>
Date: Thu, 29 Dec 2011 17:45:43 +0800
Subject: [PATCH] ADD regexp-split

---
 module/ice-9/regex.scm |   22 +++++++++++++++++++++-
 1 files changed, 21 insertions(+), 1 deletions(-)

diff --git a/module/ice-9/regex.scm b/module/ice-9/regex.scm
index f7b94b7..2ab28de 100644
--- a/module/ice-9/regex.scm
+++ b/module/ice-9/regex.scm
@@ -41,7 +41,7 @@
   #:export (match:count match:string match:prefix match:suffix
            regexp-match? regexp-quote match:start match:end match:substring
            string-match regexp-substitute fold-matches list-matches
-           regexp-substitute/global))
+           regexp-substitute/global regexp-split))
 
 ;; References:
 ;;
@@ -226,3 +226,23 @@
                         (begin
                           (do-item (car items)) ; This is not.
                           (next-item (cdr items)))))))))))
+                          
+(define regexp-split
+  (lambda (regex str)
+    (let* ([ret (fold-matches 
+		 regex str (list '() 0 0 '(""))
+		 (lambda (m prev)
+		   (let* ([ll (car prev)]
+			  [count (1+ (cadr prev))]
+			  [start (caddr prev)]
+			  [tail (match:suffix m)]
+			  [end (match:start m)]
+			  [s (string-copy str start end)]
+			  )
+		     (list `(,@ll ,s ,(match:substring m)) 
+			   count (match:end m) tail)
+		     )))] ;; end fold-matches
+	   ) ;; end let*
+      `(,@(car ret) ,(cadddr ret))
+      )))
+                                
-- 
1.7.0.4

Reply via email to