[plt-scheme] v299 and pattern matching filenames

From: Robby Findler (robby at cs.uchicago.edu)
Date: Tue Nov 9 13:20:28 EST 2004

Usually I can start with the regexp in byte form already (and there's
no need for all those set!s!):

For example, to rename a file from <whatever>.jpg to <whatever>.png, do

  ;; conv-jpg-to-png : path -> path
  (define (conv-jpg-to-png filename)
       (regexp-replace #rx#"\\.jpg$" (path->bytes filename) #".png")))


At Tue, 9 Nov 2004 10:15:27 -0800 (PST), Ron Stanonik wrote:
> On Tue, 9 Nov 2004, Robby Findler wrote:
> > You should use path->bytes and byte regexps, in this case. Use
> > path->string to display the strings to the user, but otherwise try to
> > avoid it (because not all paths can be turned into strings anymore).
> > 
> > path->bytes and bytes->path, on the other hand (modulo some windows
> > issues that may even be worked out by now) don't lose any information.
> Thanks.  Let me see if I understand.  Starting with a path and pattern,
> I convert the path and the pattern to byte strings, and then convert the
> pattern byte string to a byte-regexp.
>   (set! bpath (path->bytes path))
>   (set! bpattern (string->bytes/utf-8 pattern))
>   (set! brexp (byte-regexp bpattern))
> Perform the match 
>   (regexp-match brexp bpath)
> Then convert any result byte strings that interest me back to paths
>   (set! rpath (bytes->path bresult))
> or strings
>   (set! spath (bytes->string/utf-8 bresult))
> Correct?
> Thanks,
> Ron
> stanonik at cogsci.ucsd.edu

Posted on the users mailing list.