Regular Expressions in Scheme

(import (rnrs)
        (rnrs regex))

;; This tests whether a pattern matches a string.
(display (regexp-match? "p([a-z]+)ch" "peach"))
(newline)

;; Above we used a string pattern directly, but for
;; other regexp tasks you'll need to compile an
;; optimized regexp struct.
(define r (regexp "p([a-z]+)ch"))

;; Many procedures are available for these regexps. Here's
;; a match test like we saw earlier.
(display (regexp-match? r "peach"))
(newline)

;; This finds the match for the regexp.
(display (car (regexp-match r "peach punch")))
(newline)

;; This also finds the first match but returns the
;; start and end indexes for the match instead of the
;; matching text.
(display "idx: ")
(display (regexp-match-positions r "peach punch"))
(newline)

;; The submatch variants include information about
;; both the whole-pattern matches and the submatches
;; within those matches. For example this will return
;; information for both p([a-z]+)ch and ([a-z]+).
(display (regexp-match r "peach punch"))
(newline)

;; Similarly this will return information about the
;; indexes of matches and submatches.
(display (regexp-match-positions r "peach punch"))
(newline)

;; The 'all' variants of these functions apply to all
;; matches in the input, not just the first. For
;; example to find all matches for a regexp.
(display (regexp-match-all r "peach punch pinch"))
(newline)

;; These 'all' variants are available for the other
;; functions we saw above as well.
(display "all: ")
(display (regexp-match-all-positions r "peach punch pinch"))
(newline)

;; Our examples above had string arguments. We can also provide
;; bytevector arguments in Scheme.
(display (regexp-match? r #vu8(112 101 97 99 104))) ; "peach" as bytevector
(newline)

;; The regexp package can also be used to replace
;; subsets of strings with other values.
(display (regexp-replace r "a peach" "<fruit>"))
(newline)

;; The 'replace' variant allows you to transform matched
;; text with a given procedure.
(define (to-upper str)
  (list->string (map char-upcase (string->list str))))

(display (regexp-replace r "a peach" to-upper))
(newline)

In this Scheme translation:

  1. We use the (rnrs regex) library for regular expression support.
  2. Functions like regexp-match?, regexp-match, regexp-match-positions, etc., are used instead of the Go equivalents.
  3. We create a compiled regexp with (regexp "p([a-z]+)ch").
  4. For replacing with a function, we define a to-upper function that converts a string to uppercase.
  5. Bytevectors are used instead of byte slices for working with binary data.

Note that Scheme’s regular expression support might not be as extensive as Go’s, so some advanced features might not have direct equivalents. The example above covers the basic functionality demonstrated in the original Go code.

To run this Scheme program, you would typically save it to a file (e.g., regular-expressions.scm) and run it with a Scheme interpreter that supports the R6RS standard, as it uses the (rnrs regex) library.

For a complete reference on Scheme regular expressions, you should consult the documentation for the specific Scheme implementation you’re using, as regex support can vary between implementations.