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 eb0bb80c86c9539712b78cf8902d230e0c4e778e Mon Sep 17 00:00:00 2001
From: NalaGinrut <[email protected]>
Date: Thu, 29 Dec 2011 17:25:03 +0800
Subject: [PATCH] ADD regexp-split
---
module/ice-9/regex.scm | 23 ++++++++++++++++++++++-
1 files changed, 22 insertions(+), 1 deletions(-)
diff --git a/module/ice-9/regex.scm b/module/ice-9/regex.scm
index f7b94b7..5a90c67 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,24 @@
(begin
(do-item (car items)) ; This is not.
(next-item (cdr items)))))))))))
+
+(define regexp-split
+ (lambda (regex str)
+ (let* ([len (string-length str)]
+ [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