Utility functions and syntax constructs for dealing with regular expressions in a concise manner. Will be submitted to Guile for inclusion.
match-bind
| [Special Form] |
Match a string against a regular expression, binding lexical variables to the various parts of the match.
vars is a list of names to which to bind the parts of the match. The first variable of the list will be bound to the entire match, so the number of variables needed will be equal to the number of open parentheses (`(') in the pattern, plus one for the whole match.
consequent is executed if the given expression str matches regex. If the string does not match, alternate will be executed if present. If alternate is not present, the result of match-bind
is unspecified.
Here is a short example:
(define (star-indent line) "Returns the number of spaces until the first star (`*') in the input, or #f if the first non-space character is not a star." (match-bind "^( *)\*.*$" line (_ spaces) (string-length spaces) #f))
match-bind
compiles the regular expression regex at macro expansion time. For this reason, regex must be a string literal, not an arbitrary expression.
s/// pat subst | [Function] |
Make a procedure that performs perl-like regular expression search-and-replace on an input string.
The regular expression pattern pat is in the standard regular expression syntax accepted by make-regexp
. The substitution string is very similar to perl's s///
operator. Backreferences are indicated with a dollar sign ($), and characters can be escaped with the backslash.
s///
returns a procedure of one argument, the input string to be matched. If the string matches the pattern, it will be returned with the first matching segment replaced as per the substitution string. Otherwise the string will be returned unmodified.
Here are some examples:
((s/// "foo" "bar") "foo bar baz qux foo") ⇒ "bar bar baz qux foo" ((s/// "zag" "bar") "foo bar baz qux foo") ⇒ "foo bar baz qux foo" ((s/// "(f(o+)) (zag)?" "$1 $2 $3") "foo bar baz qux foo") ⇒ "foo oo bar baz qux foo"
s///g pat subst | [Function] |
Make a procedure that performs perl-like global search-and-replace on an input string.
The pat and subst arguments are as in the non-global s///
. See s///, for more information.
s///g
differs from s///
in that it does a global search and replace, not stopping at the first match.