Macro | Package: excl | ToC | DocOverview | CGDoc | RelNotes | FAQ | Index | PermutedIndex
| Allegro CL version 10.1 Unrevised from 10.0 to 10.1. 10.0 version

## re-lambda

Arguments: regexp bindings &body body

regexp must be a string that specifies a regular expression, or a list of such a string followed by options, or an expression that evaluates to a compiled regular expression.

re-lambda returns a closure which takes the arguments `(string &key if-does-not-match)`.

When the closure is called, it scans the given string by regexp, and if it matches, then evaluates body with the environment where the variables in bindings are bound to the substrings of the match(es). The closure returns the result(s) of the last expression of body.

bindings is like the binding form of let. Each element of bindings must be either one of the following:

- `(var integer)` ;; var binds to integer-th submatch (0 for whole).
- `(var string/symbol)` ;; var binds to the submatch named by string/symbol.
- `var` ;; var binds to the submatch named by var.

If the specified match doesn't have a value, var is bound to `nil`. You can also bind a substring before or after the specified submatch, by giving modifier like `(var integer :before)` or `(var integer :after)`.

If string doesn't match regexp, body is not evaluated, and the value given to the keyword argument if-does-not-match is returned.

If regexp is the form of `(string-re options ...)`, then options are passed to compile-re to create a compiled regular expression. It takes place at macro-expansion time, so options must only contain constant literals.

### Examples

    cl-user(26): (funcall (re-lambda "(abc|def)(.*)" ((a 0) (b 1) (c 2))
                                      (list a b c))
                           "defabc")
    ("defabc" "def" "abc")
    cl-user(27): (funcall (re-lambda "(?<foo>[a-z]+)(?<bar>\\d+)" (foo bar)
                                      (list foo bar))
                           "   acl70beta ")
    ("acl" "70")
    cl-user(28): (funcall (re-lambda "cde" ((a 0 :before) (b 0) (c 0 :after))
                             (list a b c))
                           "abcdefg")
    ("ab" "cde" "fg")
    cl-user(29):

See also re-case and re-let.

### Example

    cl-user(4): (setq f
    	      (re-lambda "([^ ]+) ([^ ]+) ([^ ]+)"
    		  ((foo 1) (bar 2) (baz 3))
    		(list foo bar baz)))
    #<Interpreted Function (unnamed) @ #x71ed7892>
    cl-user(5): (funcall f "foo the bar")
    ("foo" "the" "bar")
    cl-user(6): (re-let "([^ ]+) ([^ ]+) ([^ ]+)"
    		"foo the bar"
    		((foo 1) (bar 2) (baz 3))
    	      (list foo bar baz))
    ("foo" "the" "bar")
    cl-user(7):

    cl-user(9): (re-case "foo the barmy"
    	      ("foo a (.*)" ((it 1)) (list it))
    	      ("foo the (.*)" ((it 1)) (list it))
    	      (t :no-match))
    ("barmy")
    cl-user(10): (re-case "foo a barmy"
    	      ("foo a (.*)" ((it 1)) (list it))
    	      ("foo the (.*)" ((it 1)) (list it))
    	      (t :no-match))
    ("barmy")
    cl-user(11): (re-case "foo xx barmy"
    	      ("foo a (.*)" ((it 1)) (list it))
    	      ("foo the (.*)" ((it 1)) (list it))
    	      (t :no-match))
    :no-match
    cl-user(12):

The symbol naming this operator is also exported from the regexp package.

See The new regexp2 module in regexp.htm for further information on this macro and the regexp2 module.

Copyright (c) 1998-2022, Franz Inc. Lafayette, CA., USA. All rights reserved.
This page was not revised from the 10.0 page.
Created 2019.8.20.

|  | ToC | DocOverview | CGDoc | RelNotes | FAQ | Index | PermutedIndex
| Allegro CL version 10.1 Unrevised from 10.0 to 10.1. 10.0 version