s c h e m a t i c s : c o o k b o o k

/ PatternMatching? / Cookbook.RegexSplitInclusive

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Splitting a String, Including Matches

Problem

You want to split a string based on some pattern, but you want the matches included.

Solution

The regexp-split and pregexp-split functions don't include the sections of the string that matched the regexp provided. Sometimes it's handy to be able to split a string into parts based on some regexp, but include the matches as well.

(define (regexp-split-inclusive re str)
  (let ((unmatched-part (lambda (str next-match index)
                          (if (not next-match) 
                              (if (< index (string-length str))
                                  (list (substring str index))
                                  '())
                              (if (not (eq? index (caar next-match)))
                                  (list (substring str index (caar next-match)))
                                  '())))))
    (let loop ((parts '())
               (index 0)
               (next-match (regexp-match-positions re str)))
      (if (not next-match)
          (reverse (append (unmatched-part str next-match index) parts))
          (loop (cons (substring str (caar next-match) (cdar next-match))
                      (append (unmatched-part str next-match index) parts))
                (cdar next-match)
                (regexp-match-positions re str (cdar next-match)))))))
> (regexp-split-inclusive " +" "This is a     test")
("This" " " "is" " " "a" "     " "test")

Discussion

Contributors

-- GordonWeakliem - 28 Apr 2004

CookbookForm
TopicType: Recipe
ParentTopic: RegexRecipes
TopicOrder:

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest