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

/ Cookbook.FileNamesMatchingRegularExpression

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

Finding All Files Whose Name Match a Regular Expression

Problem

You want to find all files whose names match a traditional shell regular expression (for example .). All known as globbing.

Solution

Use the function glob below:

  (require (lib "13.ss" "srfi")
           (lib "1.ss" "srfi"))

  ;; glob->regexp : string -> regexp
  (define (glob->regexp pattern)
      (regexp
       (list->string
       (string-fold-right
        (lambda (char seed)
          (case char
            ((#\*) (cons #\. (cons #\* seed)))
            ((#\.) (cons #\\ (cons #\. seed)))
            (else (cons char seed))))
      (list #\$)
      pattern))))
  
  ;; make-glob-matcher : string -> (path -> (U #t #f))
  (define (make-glob-matcher pattern)
    (let ((rx (glob->regexp pattern)))
      (lambda (path)
        (if (regexp-match rx (path->string path))
            #t
            #f))))
  
  ;; glob : path string -> (list-of path)
  (define (glob path pattern)
    (filter (make-glob-matcher pattern)
         (directory-list path)))

For example:

> (directory-list (current-directory))
(#<path:plot.ss~> #<path:analyse.ss> #<path:analyse.ss~> #<path:plot.ss> #<path:data.ss~> #<path:data.ss>)
> (glob (current-directory) "*.ss")
(#<path:analyse.ss> #<path:plot.ss> #<path:data.ss>)

Discussion

It is simple to find files matching a regular expression. You just convert the file name to a string, and use regexp-match to test. However the regular expression syntax is different to the traditional shell regular expression syntax. For example the pattern . will not work. Hence you must convert the traditional syntax to that accepted by regexp-match. That is the function of glob->regexp. From there it is simple to construct a glob matcher from MzScheme's library of regular expression and path manipulation functions.


Comments about this recipe

Contributors

-- NoelWelsh - 01 Feb 2006

CookbookForm
TopicType: Recipe
ParentTopic: FileRecipes
TopicOrder: 999

 
 
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